Bug description
Initial migrations created with EF Core 9.0.13. After updating to 10.0.3 with no other code changes, EF Core incorrectly detects a model change for (struct-type) ComplexProperty columns.
Full demo project at https://github.com/markwijnen/EfCorePendingModelChangesIssue
Demo db context:
class DemoDbContext : DbContext
{
public DbSet<DemoEntity> Entities { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<DemoEntity>().ComplexProperty(e => e.Value);
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
base.OnConfiguring(optionsBuilder);
optionsBuilder.UseSqlite("Data Source=demo.sqlite");
}
}
class DemoEntity
{
public int Id { get; set; }
public DemoValueObject Value { get; set; }
}
struct DemoValueObject
{
public int Value { get; set; }
}
Initial migration (with Microsoft.EntityFrameworkCore.Sqlite & Microsoft.EntityFrameworkCore.Design 9.0.13 and dotnet ef migrations add EfCore09):
public partial class EfCore09 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Entities",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Value_Value = table.Column<int>(type: "INTEGER", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Entities", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Entities");
}
}
After updating to Microsoft.EntityFrameworkCore.Sqlite & Microsoft.EntityFrameworkCore.Design 10.0.3, MigrateAsync fails with PendingModelChangesWarning, see stack trace below.
EF Core seems to detect model changes for the "Value_Value" column, even though there aren't any.
Running the migration tool again (dotnet ef migrations add EfCore10) adds the following:
public partial class EfCore10 : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<int>(
name: "Value_Value",
table: "Entities",
type: "INTEGER",
nullable: false,
defaultValue: 0,
oldClrType: typeof(int),
oldType: "INTEGER",
oldNullable: true);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AlterColumn<int>(
name: "Value_Value",
table: "Entities",
type: "INTEGER",
nullable: true,
oldClrType: typeof(int),
oldType: "INTEGER");
}
}
It looks like EF Core thinks that the "Value_Value" column was nullable in the original schema (oldNullable: true in Up method and nullable: true in Down method) even though it wasn't nullable in the initial migration (nullable: false).
Detected in production with Npgsql.EntityFrameworkCore.PostgreSQL. The demo shows the same error occurring with Microsoft.EntityFrameworkCore.Sqlite.
Possibly related to #36494, but this still occurs on 10.0.3.
Your code
https://github.com/markwijnen/EfCorePendingModelChangesIssue
Stack traces
Unhandled exception. System.InvalidOperationException: An error was generated for warning 'Microsoft.EntityFrameworkCore.Migrations.PendingModelChangesWarning': The model for context 'DemoDbContext' has pending changes. Add a new migration before updating the database. See https://aka.ms/efcore-docs-pending-changes. This exception can be suppressed or logged by passing event ID 'RelationalEventId.PendingModelChangesWarning' to the 'ConfigureWarnings' method in 'DbContext.OnConfiguring' or 'AddDbContext'.
at Microsoft.EntityFrameworkCore.Diagnostics.EventDefinition`1.Log[TLoggerCategory](IDiagnosticsLogger`1 logger, TParam arg)
at Microsoft.EntityFrameworkCore.Diagnostics.RelationalLoggerExtensions.PendingModelChangesWarning(IDiagnosticsLogger`1 diagnostics, Type contextType)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.ValidateMigrations(Boolean useTransaction, String targetMigration)
at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.MigrateAsync(String targetMigration, CancellationToken cancellationToken)
at Program.<Main>$(String[] args) in D:\scratch\dotnet\EfCorePendingModelChangesIssue\EfCorePendingModelChangesIssue\Program.cs:line 4
at Program.<Main>(String[] args)
Verbose output
EF Core version
10.0.3
Database provider
Microsoft.EntityFrameworkCore.Sqlite, Npgsql.EntityFrameworkCore.PostgreSQL
Target framework
.NET 10.0
Operating system
Windows 11, Ubuntu 24.04
IDE
No response
Bug description
Initial migrations created with EF Core 9.0.13. After updating to 10.0.3 with no other code changes, EF Core incorrectly detects a model change for (struct-type) ComplexProperty columns.
Full demo project at https://github.com/markwijnen/EfCorePendingModelChangesIssue
Demo db context:
Initial migration (with Microsoft.EntityFrameworkCore.Sqlite & Microsoft.EntityFrameworkCore.Design 9.0.13 and
dotnet ef migrations add EfCore09):After updating to Microsoft.EntityFrameworkCore.Sqlite & Microsoft.EntityFrameworkCore.Design 10.0.3,
MigrateAsyncfails withPendingModelChangesWarning, see stack trace below.EF Core seems to detect model changes for the "Value_Value" column, even though there aren't any.
Running the migration tool again (
dotnet ef migrations add EfCore10) adds the following:It looks like EF Core thinks that the "Value_Value" column was nullable in the original schema (
oldNullable: trueinUpmethod andnullable: trueinDownmethod) even though it wasn't nullable in the initial migration (nullable: false).Detected in production with Npgsql.EntityFrameworkCore.PostgreSQL. The demo shows the same error occurring with Microsoft.EntityFrameworkCore.Sqlite.
Possibly related to #36494, but this still occurs on 10.0.3.
Your code
Stack traces
Verbose output
EF Core version
10.0.3
Database provider
Microsoft.EntityFrameworkCore.Sqlite, Npgsql.EntityFrameworkCore.PostgreSQL
Target framework
.NET 10.0
Operating system
Windows 11, Ubuntu 24.04
IDE
No response