具有标识“Id”的项目已存在于元数据集合中。 参数名称:item
我的所有实体都有一个基类:
public class Entity : IEntity { dynamic IEntity.Id { get { return this.Id; } set { this.Id = value; } } public TKey Id { get; set; } }
例如状态实体:
[MetadataType(typeof(StatusMetadata))] public partial class Status : Entity { public string Title { get; set; } }
当我对数据库运行查询时,我收到以下错误:“具有标识’Id’的项已经存在于元数据集合中。参数名称:item”。 有没有办法解决这个问题,或者这是由inheritance引起的问题,我不能从任何类inheritance我的实体?
这似乎是一般错误,寻找一些见解,我看到:
Two tables can have the same name for a primary key. Look at the LightSwitch tables, they all have a primary key called Id.
在http://social.msdn.microsoft.com/Forums/vstudio/en-US/bd8d47da-d1b4-4be8-a7e5-193fb5360060/the-item-with-identity-actionpk-already-exists-in-the-metadata -collection?论坛=电灯开关
因此,我查看了所有实体,并获得了一个更改了Identiy数据类型的实体,并inheritance了另一个具有int Id属性的类。
我是否更改此public new string Id { get; set; }
public new string Id { get; set; }
到public string Id { get; set; }
public string Id { get; set; }
像其他人一样删除inheritance,一切正常。
原因是您从已经具有不同类型的Id属性的类inheritance。
我在CodeMigrations中看到了同样的错误。 我有一个名为“Version”的属性字符串,我inheritance的EntityData数据类也包含byte []类型的Version属性。 这会产生与您提到的相同的错误
解决方案就是不要使用基类中已有的相同属性名称 。
它发生在我身上,因为双场FK,错误地我使用相同的场两次链接表…
尝试将“new”添加到属性中,如下所示:
[MetadataType(typeof(StatusMetadata))] public partial class Status : Entity { public new string Title { get; set; } }