0

I have table with 2 foreign keys. when I am trying to insert duplicate value (for foreign keys) it gives erroe says "An item with the same key has already been added"

below is my code

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;

Please help me

4

1 回答 1

1

错误是您将同一实体多次放入实体集合。您必须在 foreach 循环中创建新的 CheckListTrack() 实体。我猜 CheckListTrack 有它自己的主键自动增量?

于 2012-09-18T10:54:31.260 回答