As far as I see,I think you'd better check a primary key whether it exists in the DataContext first,and then do inserting。
【Sample】
using (EntityContext ec = new EntityContext())
{
var result = ec.SomeModel.Where(m=>m.PrimaryKey.Equals(your record's id to be inserted)).FirstOrDefault();
if(result==null)
{
//Do inserting here……
}
}
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Error An item with the same key has already been added.
May 06, 2012 02:26 AM|LINK
Hello ocsjack:)
As far as I see,I think you'd better check a primary key whether it exists in the DataContext first,and then do inserting。
【Sample】
using (EntityContext ec = new EntityContext()) { var result = ec.SomeModel.Where(m=>m.PrimaryKey.Equals(your record's id to be inserted)).FirstOrDefault(); if(result==null) { //Do inserting here…… } }