For the following code, getting the message:
Cannot attach an entity that already exists.
C# code:
var con = (from c in cmsContentTable where c.ContentName == contentId select c).FirstOrDefault();
cmsContentTable.Attach(con);
con.ContentData = "New Value";
cmsContentTable.Context.SubmitChanges();
Why is it giving this error – Is this because a reference to con was already made
when
var con = (from c in cmsContentTable where c.ContentName == contentId select c).FirstOrDefault();
was done?
Since you are already getting the item from the table:
The context is already ‘attached’ to the object. This is true whenever you pull an already existing object from a table.
If you remove the line:
You should be fine.
A more helpful message would be “Cannot attach to an entity that is already attached to a context”, as that is what is actually happening.