-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Drop computed columns before non-computed #25238
Conversation
src/EFCore.Relational/Migrations/Operations/DropColumnOperation.cs
Outdated
Show resolved
Hide resolved
if (string.IsNullOrWhiteSpace(((ColumnOperation)operation).ComputedColumnSql)) | ||
{ | ||
dropColumnOperations.Add(operation); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of storing this in the migrations operation, let's just use the DiffContext instead. The code here would be something like this:
if (string.IsNullOrWhiteSpace(((ColumnOperation)operation).ComputedColumnSql)) | |
{ | |
dropColumnOperations.Add(operation); | |
} | |
if (string.IsNullOrWhiteSpace(diffContext.FindColumn((DropColumnOperation)operation).ComputedColumnSql) | |
{ | |
dropColumnOperations.Add(operation); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also double-check the behavior of null
vs whitespace. I think there's an overload of HasComputedColumnSql() that marks the column as computed, but doesn't actually specify the SQL. Although, it probably throws that it's not defined before it gets here...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead of storing this in the migrations operation, let's just use the DiffContext instead.
Done, check it out (am still worried we'll need facets in SQL generation at some point for dropping stuff, but we can wait for that to happen).
Also double-check the behavior of null vs whitespace. I think there's an overload of HasComputedColumnSql() that marks the column as computed, but doesn't actually specify the SQL. Although, it probably throws that it's not defined before it gets here...
Seems so - I did a quick console test using this PR code, and got the following exception stack trace (BTW should this be model-validated?):
Unhandled exception. System.InvalidOperationException: The computed column SQL has not been specified for the column 'Name.Blogs'. Specify the SQL before using Entity Framework to create the database schema.
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Initialize(ColumnOperation columnOperation, IColumn column, RelationalTypeMapping typeMapping, Boolean isNullable, IEnumerable`1 migrationsAnnotations, Boolean inline) in /home/roji/projects/efcore/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:line 1145
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(IColumn target, DiffContext diffContext, Boolean inline)+MoveNext() in /home/roji/projects/efcore/src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs:line 1090
Test code
await using var ctx = new BlogContext();
await ctx.Database.EnsureDeletedAsync();
await ctx.Database.EnsureCreatedAsync();
public class BlogContext : DbContext
{
public DbSet<Blog> Blogs { get; set; }
static ILoggerFactory ContextLoggerFactory
=> LoggerFactory.Create(b => b.AddConsole().AddFilter("", LogLevel.Information));
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder
.UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0")
.EnableSensitiveDataLogging()
.UseLoggerFactory(ContextLoggerFactory);
protected override void OnModelCreating(ModelBuilder modelBuilder)
=> modelBuilder.Entity<Blog>().Property(b => b.Name).HasComputedColumnSql();
}
public class Blog
{
public int Id { get; set; }
public string Name { get; set; }
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, it's a valid model--we just can't create the full schema for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, that seems... nuanced... But doesn't really matter.
1467c3f
to
4c231ff
Compare
src/EFCore.Relational/Migrations/Internal/MigrationsModelDiffer.cs
Outdated
Show resolved
Hide resolved
4c231ff
to
009c858
Compare
Hello @roji! Because this pull request has the p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (
|
Apologies, while this PR appears ready to be merged, I've been configured to only merge when all checks have explicitly passed. The following integrations have not reported any progress on their checks and are blocking auto-merge:
These integrations are possibly never going to report a check, and unblocking auto-merge likely requires a human being to update my configuration to exempt these integrations from requiring a passing check. Give feedback on thisFrom the bot dev teamWe've tried to tune the bot such that it posts a comment like this only when auto-merge is blocked for exceptional, non-intuitive reasons. When the bot's auto-merge capability is properly configured, auto-merge should operate as you would intuitively expect and you should not see any spurious comments. Please reach out to us at [email protected] to provide feedback if you believe you're seeing this comment appear spuriously. Please note that we usually are unable to update your bot configuration on your team's behalf, but we're happy to help you identify your bot admin. |
Fixes #25237