efcore: Exception when creating new migration after updating from 6.0-rc.1 to 6.0-rc.2 or 6.0-rtm (daily)

I’ve updated my test solution from net6.0-rc.1 and efcore6.0-rc.1 to rc.2 versions. Then I try to add a new migration that drops a table from model class CloudManagementTool.DataAccess.Model.Supplier.SupInvoiceReconciliationFile and should create a new table from model class CloudManagementTool.DataAccess.Model.Supplier.SupInvoice.

When running the Add-Migration command from Package Manager Console, I get the following exception:

System.ArgumentException: An item with the same key has already been added. Key: [0, Property: CloudManagementTool.DataAccess.Model.Supplier.SupInvoiceReconciliationFile (Dictionary<string, object>).TenantID (no field, long) Indexer Required FK Index]
   at System.Collections.Generic.TreeSet`1.AddIfNotPresent(T item)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(IEntityType entityType, ITable table)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedColumns(ITable table)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(ITable target, DiffContext diffContext)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
An item with the same key has already been added. Key: [0, Property: CloudManagementTool.DataAccess.Model.Supplier.SupInvoiceReconciliationFile (Dictionary<string, object>).TenantID (no field, long) Indexer Required FK Index]

As the class from the error no longer exists in the code, I have no idea how to fix or workaround this error.

I cannot find any error in the existing migrations, in the class for the new table and in the existing code. The previous code worked as expected. Then I downgraded netcore6.0 and efcore6.0 back to rc.1 and using that version, adding a new migration works without any issues.

PM> Add-Migration RefactorSupplier
Build started...
Build succeeded.
An operation was scaffolded that may result in the loss of data. Please review the migration for accuracy.
To undo this action, use Remove-Migration.

Working EF Core Product Version: 6.0.0-rc.1.21452.10 Failing EF Core Product Version: 6.0.0-rc.2.21480.5

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 9
  • Comments: 40 (16 by maintainers)

Commits related to this issue

Most upvoted comments

I also got this error when dropping a table and some related tables. A solution that worked for me when removing an entity and getting this error was to

  1. Remove one navigation property (and corresponding foreign key) at a time from the entity and create a migration for each removal.
  2. When no more navigation properties were left in the entity I could remove it and create a final migration without the error message.

In my case I couldn’t wait for a new release in february. I spent a whole day with trial and errors before succeeding

@davidkarlsson We will discuss when all the required people are available.

I get this a lot and I think this should be addressed ASAP.

I get this almost everything when I’m trying to delete or rename the table. @ajcvickers

System.ArgumentException: An item with the same key has already been added. Key: [0, Property: Grey.Models.ProductQuestion (Dictionary<string, object>).ProductId (no field, long) Indexer Required FK Index]
   at System.Collections.Generic.TreeSet`1.AddIfNotPresent(T item)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(IEntityType entityType, ITable table)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedColumns(ITable table)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(ITable target, DiffContext diffContext)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

This is not the exact scenario as above but I get the same stack trace. I think I have a repro here: https://github.com/SmithE65/TableRename

Maybe not as minimal as could be.

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions options) : base(options)
    {
    }

    public DbSet<Entity1>? Entity1s { get; set; }
    public DbSet<Entity2>? Entity2s { get; set; }
    public DbSet<Entity3>? Entity3s { get; set; }
}

// ....

public class Entity1
{
    public int Id { get; set; }
    public int Entity2Id { get; set; }
    [ForeignKey(nameof(Entity2Id))]
    public Entity2? Entity2Navigation { get; set; }
    public int Entity3Id { get; set; }
    [ForeignKey(nameof(Entity3Id))]
    public Entity3? Entity3Navigation { get; set; }
}

public class Entity2
{
    public int Id { get; set; }
    [InverseProperty(nameof(Entity1.Entity2Navigation))]
    public ICollection<Entity1> Entity1s { get; set; } = new List<Entity1>();
}

public class Entity3
{
    public int Id { get; set; }
    [InverseProperty(nameof(Entity1.Entity3Navigation))]
    public ICollection<Entity1> Entity1s { get; set; } = new List<Entity1>();
}

I create the three entities as above, create an Initial migration, rename Entity1 class and rename Entity1s DbSet, and attempt to create a second migration. Migration fails with below exception and trace:

System.ArgumentException: An item with the same key has already been added. Key: [0, Property: TableRename.Entity1 (Dictionary<string, object>).Entity3Id (no field, int) Indexer Required FK Index]
   at System.Collections.Generic.TreeSet`1.AddIfNotPresent(T item)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedProperties(IEntityType entityType, ITable table)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetSortedColumns(ITable table)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Add(ITable target, DiffContext diffContext)+MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.DiffCollection[T](IEnumerable`1 sources, IEnumerable`1 targets, DiffContext diffContext, Func`4 diff, Func`3 add, Func`3 remove, Func`4[] predicates)+MoveNext()
   at System.Linq.Enumerable.ConcatIterator`1.MoveNext()
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.Sort(IEnumerable`1 operations, DiffContext diffContext)
   at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationsModelDiffer.GetDifferences(IRelationalModel source, IRelationalModel target)
   at Microsoft.EntityFrameworkCore.Migrations.Design.MigrationsScaffolder.ScaffoldMigration(String migrationName, String rootNamespace, String subNamespace, String language)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType, String namespace)
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_0.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
An item with the same key has already been added. Key: [0, Property: TableRename.Entity1 (Dictionary<string, object>).Entity3Id (no field, int) Indexer Required FK Index]

If I rename just the Entity1 class, create an empty migration, rename the DbSet, and then create a migration with the table rename, everything works as expected. I have to change BOTH for the migration to fail.

@Tragetaschen Agreed. However, you can use the 7.0.0 daily build, which has the 6.0.2 patches in it, and not much else at this point in the release cycle.

I know it’s fixed and scheduled for Release, but not having access to a preview package for the patch release to unblock the scenarios that are affected is… annoying (see https://github.com/dotnet/efcore/issues/26632#issuecomment-989896297). I wish there was a better way than waiting for several weeks 😕

In my case I was replacing the term Download with Asset in my whole model, with the term being used in foreign keys, tables, navigation properties etc.

dotnet ef migrations add ... raised the mentioned exception.

Workaround

  1. Rename foreign keys, navigation properties, collections etc as you wish
  2. In the entity type configurations, ensure the entity-table-names containing this name are still using the old name (i.e. “Downloads”, “DownloadTranslations”) - read: ensure all .ToTable(...) expressions use the old name, and, if they don’t exist, add them.
    (I think foreign key names etc. can already use the new name)
  3. Create a new migration with dotnet ef migrations add ... - no exception
  4. Check generated migration code (no unwanted drops etc)
  5. Rename the tables to their new names (i.e. “Assets”, “AssetTranslations”)
  6. Create a new migration with dotnet ef migrations add ... - no exception
  7. Check generated migration code (no unwanted drops etc)

Word of Caution

Always have a backup or an integration database to test if the migrations are working as expected, even if the tool doesn’t throw an exception.

Infos

EF Core version: 6.0.1 EF Core tool version: 6.0.1

I’m also getting this same error when attempting to add a migration that should remove five tables (stack trace is identical to what @christallire posted, just a different property in the exception message). I will try the manual workaround of modifying the snapshot file and then putting the code in an empty migration. It would be great if this were fixed sooner than EF Core 7.

Confirmed repro.

@schuettecarsten It’s very hard to tell what is going on with just the snippets of the snapshots. I know it may take some work, but if you can, then please attach a small, runnable project or post a small, runnable code listing that reproduces what you are seeing so that we can investigate.

It seems to work with 6.0.0-rtm.21519.8