Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot attach an entity that already exists

Tags:

c#

linq-to-sql

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();
like image 278
coure2011 Avatar asked Apr 09 '10 07:04

coure2011


1 Answers

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(); 
like image 153
bniwredyc Avatar answered Sep 20 '22 22:09

bniwredyc