efcore: Add-Migration fails with "An item with the same key has already been added" when renaming entity
I made this change to my DbContext and now Add-Migration fails:
- public DbSet<DbImport> Imports => Set<DbImport>();
+ public DbSet<DbPageImport> PageImports => Set<DbPageImport>();
In words: I renamed the DbImport entity class AND renamed the Imports table.
Include your code
I have tried to create a minimal reproduction of the issue but can’t get the error to occur. If necessary, I can strip out everything from my real codebase until I have a small project that demonstrates the bug (but this would be a lot of work).
Both me and my coworker are able to consistently reproduce the bug on our real closed-source codebase. I am happy to send this codebase to any EF Core contributor as a private GitHub repo.
Include verbose output
Output from Add-Migration in Package Manager Console in Visual Studio 2022:
PM> add-migration TestMigration -Verbose
Using project 'Backend\Data\DataContext'.
Using startup project 'Backend\Functions\FunctionApp'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\Projects\System7\Slateplan\Backend\Functions\FunctionApp\bin\Debug\net6.0\FunctionApp.deps.json --additionalprobingpath C:\Users\SamMa\.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\Projects\System7\Slateplan\Backend\Functions\FunctionApp\bin\Debug\net6.0\FunctionApp.runtimeconfig.json C:\Users\SamMa\.nuget\packages\microsoft.entityframeworkcore.tools\6.0.2\tools\netcoreapp2.0\any\ef.dll migrations add TestMigration --json --verbose --no-color --prefix-output --assembly C:\Projects\System7\Slateplan\Backend\Functions\FunctionApp\bin\Debug\net6.0\DataContext.dll --project C:\Projects\System7\Slateplan\Backend\Data\DataContext\DataContext.csproj --startup-assembly C:\Projects\System7\Slateplan\Backend\Functions\FunctionApp\bin\Debug\net6.0\FunctionApp.dll --startup-project C:\Projects\System7\Slateplan\Backend\Functions\FunctionApp\FunctionApp.csproj --project-dir C:\Projects\System7\Slateplan\Backend\Data\DataContext\ --language C# --configuration Debug --working-dir C:\Projects\System7\Slateplan --root-namespace DataContext --nullable
Using assembly 'DataContext'.
Using startup assembly 'FunctionApp'.
Using application base 'C:\Projects\System7\Slateplan\Backend\Functions\FunctionApp\bin\Debug\net6.0'.
Using working directory 'C:\Projects\System7\Slateplan\Backend\Functions\FunctionApp'.
Using root namespace 'DataContext'.
Using project directory 'C:\Projects\System7\Slateplan\Backend\Data\DataContext\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider in assembly 'FunctionApp'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
Finding DbContext classes in the project...
Found DbContext 'AppDataContext'.
Using context 'AppDataContext'.
Finding design-time services referenced by assembly 'FunctionApp'...
Finding design-time services referenced by assembly 'DataContext'...
No referenced design-time services were found.
Finding design-time services for provider 'Microsoft.EntityFrameworkCore.SqlServer'...
Using design-time services from provider 'Microsoft.EntityFrameworkCore.SqlServer'.
Finding IDesignTimeServices implementations in assembly 'FunctionApp'...
No design-time services were found.
The index {'OrganizationId'} was not created on entity type 'DbComponentType' as the properties are already covered by the index {'OrganizationId', 'Name'}.
The index {'ComponentId'} was not created on entity type 'DbComponentVersion' as the properties are already covered by the index {'ComponentId', 'VersionName'}.
The index {'PageId'} was not created on entity type 'DbDesignerData' as the properties are already covered by the index {'PageId', 'Type'}.
The index {'OrganizationId'} was not created on entity type 'DbLogoSet' as the properties are already covered by the index {'OrganizationId', 'Name'}.
The index {'OrganizationId'} was not created on entity type 'DbProductFamily' as the properties are already covered by the index {'OrganizationId', 'Name'}.
The index {'ProductKitId'} was not created on entity type 'DbProductKitVersion' as the properties are already covered by the index {'ProductKitId', 'VersionName'}.
The index {'OrganizationId'} was not created on entity type 'DbProductPhoto' as the properties are already covered by the index {'OrganizationId', 'Name'}.
The index {'OrganizationId'} was not created on entity type 'DbProject' as the properties are already covered by the index {'OrganizationId', 'Name'}.
The index {'ProjectId'} was not created on entity type 'DbProjectPublication' as the properties are already covered by the index {'ProjectId', 'RevisionNumber'}.
The index {'OrganizationId'} was not created on entity type 'DbSymbol' as the properties are already covered by the index {'OrganizationId', 'Name'}.
The index {'OrganizationId'} was not created on entity type 'DbTermsDocument' as the properties are already covered by the index {'OrganizationId', 'Number'}.
The property 'DbComponentVersion.SellPrice#Money.DbComponentVersionId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbComponentVersion.Url#Url.DbComponentVersionId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbLogoSet.DarkLogo#DbFileRef.DbLogoSetId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbLogoSet.LightLogo#DbFileRef.DbLogoSetId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbPage.Pdf#DbFileRef.DbPageId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbPage.Thumbnail#DbFileRef.DbPageId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbPageImport.File#DbFileRef.DbPageImportId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbPageImport.PercentComplete#Percentage.DbPageImportId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbProductKitVersion.SellPrice#Money.DbProductKitVersionId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbProductPhoto.Photo#DbFileRef.DbProductPhotoId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbProject.Photo#DbFileRef.DbProjectId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbReport.File#DbFileRef.DbReportId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbReport.PercentComplete#Percentage.DbReportId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbTermsDocument.File#DbFileRef.DbTermsDocumentId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbUser.Email#EmailAddress.DbUserId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'DbProjectReportOptions.DbProjectId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'EncodedPassword.DbUserId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'PhoneNumber.CompanyContactInfoDbProjectReportOptionsDbProjectId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'CompanyContactInfo.DbProjectReportOptionsDbProjectId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'CompanyContactInfo.Email#EmailAddress.CompanyContactInfoDbProjectReportOptionsDbProjectId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'CompanyContactInfo.Url#Url.CompanyContactInfoDbProjectReportOptionsDbProjectId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'OrganizationShortName.DbOrganizationId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'PartialAddress.DbProjectId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'PersonName.DbUserId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'PostalCode.PartialAddressDbProjectId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'ProjectBudgetOptions.DbProjectId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'ProjectBudgetOptions.CostAdjustment#Percentage.ProjectBudgetOptionsDbProjectId' was created in shadow state because there are no eligible CLR members with a matching name.
The property 'ProjectBudgetOptions.DepositPercentage#Percentage.ProjectBudgetOptionsDbProjectId' was created in shadow state because there are no eligible CLR members with a matching name.
DetectChanges starting for 'AppDataContext'.
DetectChanges completed for 'AppDataContext'.
DetectChanges starting for 'AppDataContext'.
DetectChanges completed for 'AppDataContext'.
'AppDataContext' disposed.
System.ArgumentException: An item with the same key has already been added. Key: System.Object Item [System.String]
at System.Collections.Generic.Dictionary`2.TryInsert(TKey key, TValue value, InsertionBehavior behavior)
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: System.Object Item [System.String]
Include provider and version information
EF Core version: 6.0.2 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0 Operating system: Windows 11 IDE: Visual Studio 2022 17.1.0
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 8
- Comments: 30 (8 by maintainers)
Commits related to this issue
- Filter out indexer navigations when sorting columns Fixes #27504 — committed to dotnet/efcore by AndriySvyryd a year ago
- Filter out indexer navigations when sorting columns Fixes #27504 — committed to dotnet/efcore by AndriySvyryd a year ago
- [release/7.0] Filter out indexer navigations when sorting columns Fixes https://github.com/dotnet/efcore/issues/27504 — committed to dotnet/efcore by AndriySvyryd a year ago
- Reset migrations Needed to purge migrations and database due to bug: https://github.com/dotnet/efcore/issues/27504 — committed to GodVenn/flotilla by GodVenn a year ago
- Reset migrations Needed to purge migrations and database due to bug: https://github.com/dotnet/efcore/issues/27504 — committed to equinor/flotilla by GodVenn a year ago
I just got this error after doing major code refactoring. The scenario is There was AgentBooking Entity. Now the business decide to add OnlineBooking, Then I need to refactor AgentBooking & OnlineBooking to inherit from Abstract Booking Entity and rename some columns.
This error appear, if I remove all previous migrations & DbContextModelSnapshot, then recreate one it will be successful. But there is no way to run this migration in production environment.
Is there anyway to debug migrations tools? I need to find out where is the problem. Key: System.Object Item [System.String] is kinda unhelpful at all.
This just hit us on a large project that has recently been upgraded to .net7 and EF Core 7.01. We are refactoring to remove all OwnedEntities. We removed one, successfully added a migration, then spent hours factoring out dozens of other owned entites. Then we tried to add another migration and this error hit.
The error is so vague it gives no clue how to proceed.
Ah sorry, missed that. The version of the CLI tool doesn’t usually matter. Make sure your project is using the latest daily build packages.
@AndriySvyryd I just tested the latest dotnet ef cli, that I think should contain the fixe https://github.com/dotnet/efcore/pull/29981, and I still get the error.
I have a table that contains owned types and this table is not registered with a
DbSet<>but only by aIEntityTypeConfiguration<>. When removing the configuration I get the same error as everybody.Was it supposed to work or is it the same message for an other error? Thank you!
My error, and how I resolved it
I encountered the same error message when using
Entity Framework Core .NET Command-line Tools 7.0.1in a project that depended on versions6.0.9of each of the various Entity Framework Core packages. This is an oversimplification of my model, but it shows all the parts that were relevant to solving my error.Code
DbContext snippet
Foo snippet
Bar snippet
The problem
Removing the
Fooclass and any references to it and then runningdotnet ef migrations add MigrationNameresulted in the same error message as reported by others in this thread.How I resolved it
Prop1andProp2fromFooDbSet<Foo>property from theDbContextConclusion
I am relatively new to Entity Framework Core, so I cannot diagnose what initially caused this error, but I assume it has something to do with deleting entities that have owned entities attached.
The one line diff I posted above should avoid the exception. But a better fix would sort columns without using
PropertyInfoas a dictionary key.For my own project I replaced the model differ service to test the one line code change. However, overriding the implementation of
MigrationsModelDiffer.GetSortedPropertiesinvolved copying huge slabs ofinternalcode. Not really viable as a workaround for production code. Though you only need this fix when generating migrations, you don’t need to deploy any of this code to production.Seeing the same in 6.0.1 caused by removing an entity (and the associated DbSet on the DbContext) that had an owned entity.
I was able to work around this by removing the representation of said entity in the snapshot, scaffolding an empty migration and populating the respective Up and Down from the initial migration that was already present.
I got the same error, I’m using entities with owned entities. If I remove the DbSets the generated migrations are empty. If I remove the DbSets and fluent-maps, it throw the error.
Got the same when adding a migration that removes two completely independent DbSets (flat tables no relationships etc).
Hello, I am getting same error, when I try to create migration after removing entity with owned entities. Version of all EFCore related nugets + tools: 6.0.7 In this example project EfMigrationIssueOwnedEntities.zip you can get error by using commands: When I try to add migration either by
dotnet ef migrations add DroFooorAdd-Migration DropFooin package manager console, i am getting same error:There is workaround:
Is it fixed on 6.0.2? I’m still getting it on 6.0.4
@srmagura upgrading to 6.0.2 as per @bricelam above, fixed issue for me
Sounds good, I will work on it. It may be about 2 weeks — I’ll comment here when it is ready.