我有2个外键的表。当我试图插入重复值(对于外键)时,它给erroe说“已经添加了具有相同键的项目”
下面是我的代码
CheckListTrack CheckListtrack = new CheckListTrack();
trackId = entityCollection.WorkItemTrack.Max(p => p.TrackID) ;
foreach (int ListID in tracklist)
{
CheckListtrack.WorkItemTrackReference.EntityKey = new EntityKey("SEIESSEntities.WorkItemTrack", "TrackID", trackId); // foreign key
CheckListtrack.CheckListReference.EntityKey = new EntityKey("SEIESSEntities.CheckList", "CheckListID", ListID); // foreign key
entityCollection.AddToCheckListTrack(CheckListtrack); // error comes after second time control passes here
}
curentScope.Complete();
success = true;
请帮帮我
答案 0 :(得分:1)
错误在于您将同一实体多次放入实体集合中。您必须在foreach循环中创建新的CheckListTrack()实体。我猜CheckListTrack有自己的主键自动增量?