The item with identity 'Id' already exists in the metadata collection. Parameter name: item
Solution 1
It seems a generic error, searching for some insights, I saw that:
Two tables can have the same name for a primary key. Look at the LightSwitch tables, they all have a primary key called Id.
So, I review all entities and I get an entity with a Identiy data type changed and a inherit another class it has int Id property.
Do I change this public new string Id { get; set; }
to public string Id { get; set; }
like others and remove the inherit and everything works fine.
Solution 2
The reason is because you inherit from a class that already has an Id property of a different type.
I have seen the same error in CodeMigrations. I had a property named "Version" of type string, and the EntityData data class where I was inheriting from also containes a Version property of type byte[]. This generated the same error as you mentioned
The solution for this is just to don't use the same property names that are already in your base class.
beardeddev
Updated on June 12, 2022Comments
-
beardeddev almost 2 years
All of my entities have a base class:
public class Entity<TKey> : IEntity<TKey> { dynamic IEntity.Id { get { return this.Id; } set { this.Id = value; } } public TKey Id { get; set; } }
For example Status entity:
[MetadataType(typeof(StatusMetadata))] public partial class Status : Entity<byte> { public string Title { get; set; } }
When I run the query against the database I get the following error: "The item with identity 'Id' already exists in the metadata collection. Parameter name: item". Is there any way to fix this or it's an issue caused by inheritance and I can't inherit my entities from any class?
-
phil soady about 10 yearsdid you try this in EF6 ?
-
CedricPoilly about 10 yearsI have not used it yet. The issue your are facing might be due to the fact that you are defining a Title property in a class that already inherit a Type property for the Entity class :)
-
phil soady about 10 yearsI dont think EF support parameter hiding. It didnt in EF5.x I havent tried in ef6.x eg stackoverflow.com/q/18245686/1347784
-
beardeddev about 10 yearsAs you can see in my example, the base class doesn't have property Title, but it has property Id, that are not defined in inherited class.
-
Anthony over 8 yearsI was having this problem with EF 6.1.2. I upgraded to EF 6.1.3 and the problem magically disappeared.
-
Brett Postin over 8 yearsConfirmed. Property hiding appears to be supported in EF 6.1.3.