The item with identity [x] already exists in the metadata collection. How do I fix that?

.net c# entity-framework entity-framework-6 poco

Question

I'm using Entity Framework 6, with POCO and fluent-API and I've noticed an annoying bug.

If I have an entity called MyEntity and this entity has a property called MyProp, that makes it impossible to create an entity called MyEntity_MyProp.

Exception:

The item with identity 'MyEntity_MyProp' already exists in the metadata collection.\r\nParameter name: item

The error immediately goes away if I rename any of the entities, or rename the properties.

The "bug" is obvious: the key [EntityName]_[PropertyName] must be unique in the metadata collection.

Screenshot:

Image

I'm migrating a huge Entity Framework model with 390+ classes from EF 4, database first, to EF 6, code first, with fluent-API. It's out of question to rename the entities or the tables.

How do I solve that?

EDIT

This SO question doesn't have anything to do with my problem: The item with identity 'Id' already exists in the metadata collection. Parameter name: item

1
10
5/23/2017 12:16:43 PM

Accepted Answer

It's a known bug. It's currently scheduled to be fixed in an arbitrary future version, that is, it's not in the road-map yet.

Source: https://entityframework.codeplex.com/workitem/2084

EDIT:

According to @Anthony, this was fixed in v6.1.3

8
7/18/2017 7:28:07 PM

Popular Answer

This bug happens when you use underscores in the name of your entities. The reason is Entity Framework also uses underscores to create the names of the keys (concatenating the entity and property names).

So, if you have an entity named "Table" with a property "Prop" and a table named "Table_Prop" a clash will occur. This is most likely what happened.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow