coure2011
coure2011

Reputation: 42434

Cannot attach an entity that already exists

I am trying to update code via Linq, but I am getting this error:

Cannot attach an entity that already exists.

C# code is here:

var con = (from c in cmsContentTable where c.ContentName == contentId
           select c).FirstOrDefault();  
cmsContentTable.Attach(con);  
con.ContentData = "New Value";  
cmsContentTable.Context.SubmitChanges();

Upvotes: 6

Views: 5990

Answers (1)

bniwredyc
bniwredyc

Reputation: 8829

You don't need to attach the entity, it already belongs to the context.

var con = (from c in cmsContentTable where c.ContentName == contentId select c).FirstOrDefault();
con.ContentData = "New Value";
cmsContentTable.Context.SubmitChanges(); 

Upvotes: 16

Related Questions