question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Migration : An item with the same key has already been added

See original GitHub issue

With the rc1 and rc2 the migration generate an error “An item with the same key has already been added”, the problem is that the message not indicate the code which throw the exception.

System.ArgumentException: An item with the same key has already been added.
  at System.ThrowHelper.ThrowArgumentException (ExceptionResource resource) <0x7f064a680550 + 0x00021> in <filename unknown>:0 
  at System.Collections.Generic.Dictionary`2[TKey,TValue].Insert (System.Collections.Generic.TKey key, System.Collections.Generic.TValue value, Boolean add) <0x7f064a500a30 + 0x00168> in <filename unknown>:0 
  at System.Collections.Generic.Dictionary`2[TKey,TValue].Add (System.Collections.Generic.TKey key, System.Collections.Generic.TValue value) <0x7f064a4ffbe0 + 0x0001b> in <filename unknown>:0 
  at Microsoft.Data.Entity.Migrations.Design.CSharpMigrationOperationGenerator.Generate (Microsoft.Data.Entity.Migrations.Operations.CreateTableOperation operation, Microsoft.Data.Entity.Internal.IndentedStringBuilder builder) <0x423a9000 + 0x003c5> in <filename unknown>:0 
  at (wrapper dynamic-method) System.Object:CallSite.Target (System.Runtime.CompilerServices.Closure,System.Runtime.CompilerServices.CallSite,Microsoft.Data.Entity.Migrations.Design.CSharpMigrationOperationGenerator,object,Microsoft.Data.Entity.Internal.IndentedStringBuilder)
  at Microsoft.Data.Entity.Migrations.Design.CSharpMigrationOperationGenerator.Generate (System.String builderName, IReadOnlyList`1 operations, Microsoft.Data.Entity.Internal.IndentedStringBuilder builder) <0x42358cb0 + 0x001fc> in <filename unknown>:0 
  at Microsoft.Data.Entity.Migrations.Design.CSharpMigrationsGenerator.GenerateMigration (System.String migrationNamespace, System.String migrationName, IReadOnlyList`1 upOperations, IReadOnlyList`1 downOperations) <0x42354f90 + 0x004c0> in <filename unknown>:0 
  at Microsoft.Data.Entity.Migrations.Design.MigrationsScaffolder.ScaffoldMigration (System.String migrationName, System.String rootNamespace, System.String subNamespace) <0x4231a670 + 0x006cf> in <filename unknown>:0 
  at Microsoft.Data.Entity.Design.MigrationsOperations.AddMigration (System.String name, System.String outputDir, System.String contextType) <0x42265e50 + 0x0011e> in <filename unknown>:0 
  at Microsoft.Data.Entity.Commands.Program+Executor+<>c__DisplayClass6_0.<AddMigration>b__0 () <0x41dd3840 + 0x00034> in <filename unknown>:0 
  at Microsoft.Data.Entity.Commands.Program+Executor.Execute (System.Action action) <0x41dd37b0 + 0x00028> in <filename unknown>:0 
An item with the same key has already been added.

The entities are generated correctly with beta8.

But now apparently the migration implement automatically the discriminator in the whole of entities, that’s what i see when execute an action with loading entities on the old table with a hierarchy. I use a base class with the common columns that is inherited by the others, and perhaps EF want to generate a table with the base class to respect the TPH and which will contain all entities by default, before only the entity with an explicit discriminator with the fluent api was generated and the other entities have the hierarchy into the table with the name of derived class.

An another issue to refer to this message #2170

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
tomchovaneccommented, Jul 27, 2017

In my case it was caused by duplicit IDbContextFactory<MyDbContext> implementation (twice, in 2 different files).

0reactions
bricelamcommented, Dec 11, 2015

Oh sorry, I was closing it because it was a duplicate of #3061. #3061 will stay open until it’s fixed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add-Migration fails with "An item with the same key has ...
The error is so vague it gives no clue how to proceed. System.ArgumentException: An item with the same key has already been added....
Read more >
Entity Framework Core Add-Migration throws "An item with ...
Entity Framework Core Add-Migration throws "An item with the same key has already been added. Key: MyProject.Model.MyContext · Ask Question.
Read more >
Error "An item with the same key has already been added Key ...
Error "An item with the same key has already been added Key" Occurs When migrating one workbook and one published data source by...
Read more >
SPMT - Some Documents don't migrate with error "An item ...
The error is "An item with the same key has already been added" but none of the documents exist in the target list....
Read more >
Error "An item with the same key has already been added"
This article helps to fix the error "An item with the same key has already been added". Applies to: Windows Server 2012 R2...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found