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

耗尽温柔 提交于 2019-12-05 06:00:31

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.

At 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?forum=lightswitch

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.

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.

It happened to me because of a double field FK, that mistakenly I used the same field twice to link the tables...

Try adding 'new' to the property like this:

[MetadataType(typeof(StatusMetadata))]
public partial class Status : Entity<byte>
{
       public new string Title { get; set; }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!