efcore icon indicating copy to clipboard operation
efcore copied to clipboard

EF Core Sqlite - Migration doesn't apply configuration when [NotMapped] property is added to one of the DbSets

Open mhenkrich-wh opened this issue 1 year ago • 3 comments

Hi,

I am getting different output for migration files when I include [NotMapped] property to one of the DbSets.

When I call migration I get:

  migrationBuilder.CreateTable(
      name: "Defects",
      columns: table => new
      {
          Id = table.Column<string>(type: "varchar(36)", nullable: false),
          LastModifiedDateUtc = table.Column<long>(type: "bigint", nullable: false),
          LastClientModifiedDateUtc = table.Column<long>(type: "bigint", nullable: true),
          WorkStepId = table.Column<string>(type: "varchar(36)", nullable: true)
      },

When I add not mapped entity to one of the DbSets,:

[NotMapped]
public IEnumerable<EntryEf> Entries => Defects.Cast<EntryEf>().Concat(Expenses);

Then when I delete migration and create new one the output of the new migration is:

     migrationBuilder.CreateTable(
         name: "Defects",
         columns: table => new
         {
             Id = table.Column<Guid>(type: "TEXT", nullable: false),
             LastModifiedDateUtc = table.Column<DateTime>(type: "TEXT", nullable: false),
             LastClientModifiedDateUtc = table.Column<DateTime>(type: "TEXT", nullable: true),
             WorkStepId = table.Column<string>(type: "varchar(36)", nullable: true)
         },

Which is completely different, and I added only [NotMapped] property.

In order to fully reproduce bug I created repo: https://github.com/mhenkrich-wh/EntityFrameworkCore_Bug_11_27_24

Steps to reproduce:

  1. Build -> Add migration "Add-migration test1" -> inspect migration.
  2. In file WorkStep.cs comment out lines 22 and 23 (this mapped property)
  3. Delete current migration
  4. Build -> Add migration "Add-migration test2" -> inspect migration.

You can see that test1 migration output doesn't match test2 migration output. To me it seems like custom configuration is not applied.

mhenkrich-wh avatar Nov 27 '24 15:11 mhenkrich-wh

@cincuranet FYI I'm doing some investigation on this one.

ajcvickers avatar Nov 29 '24 14:11 ajcvickers

Note for team: confirmed repro, same behavior on EF9. This looks like a layering issue in model building. When the Entries is present, then it brings EntryEf as a mapped abstract base class (it is not mapped otherwise). The [NotMapped] attribute causes EntryEf to then be removed from the model again, which is correct, but the state of annotations is different (not fully reverted), which ultimately produces a different result for some of the derived classes.

@mhenkrich-wh As a workaround, Ignore the EntryEf as the first line of OnModelCreating. This will prevent it ever being included in the model.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Ignore<EntryEf>();
    ...    

ajcvickers avatar Nov 29 '24 15:11 ajcvickers

Would be fixed by #15898

AndriySvyryd avatar Jan 30 '25 01:01 AndriySvyryd