Skip to content

Add-Migration fails with "An item with the same key has already been added" when renaming entity #27504

@srmagura

Description

@srmagura

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

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions