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

c# entity-framework entity-framework-6

Question

My entities share the following basic class:

public class Entity<TKey> : IEntity<TKey>
{
    dynamic IEntity.Id
    {
        get
        {
            return this.Id;
        }
        set
        {
            this.Id = value;
        }
    }

    public TKey Id { get; set; }
}

Examples of Status entities

[MetadataType(typeof(StatusMetadata))]
public partial class Status : Entity<byte>
{
       public string Title { get; set; }
}

I see the following error when I run the database query: "The metadata collection already has the item with identification "Id." the parameter item ". Is there a method to fix this, or is there a problem with inheritance that prevents me from deriving my entities from any class?

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

Popular Answer

You inherit from a class that already has an Id property of a different type, which is the cause.

The identical problem has appeared for me in CodeMigrations. I had a string-type property called "Version," and the EntityData data class from which I was deriving also contained a Version property of byte type[. The exact mistake that you stated was produced by this.

There is only one way to solve this: utilize different property names than those 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