The item with identity 'Id' already exists in the metadata collection. Parameter name: item

c# entity-framework entity-framework-6

Question

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?

1
6
12/5/2014 9:38:18 PM

Popular Answer

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.

5
2/2/2015 1:59:21 PM


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