26 Temmuz 2012 Perşembe

update error in dbcontext (ef)

When i tried to update an entity by using DbContext (entity framework), i got this error:

{"An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key."}

The discussion in http://stackoverflow.com/questions/6033638/an-object-with-the-same-key-already-exists-in-the-objectstatemanager-the-object helped me to solve it.
I replaced the lines


//context.Sentence.Attach(temp);
// context .Entry(temp).State = System.Data.EntityState.Modified;


with the following:


Sentence old =  context.Sentence.Find(temp.ID);
context .Entry(old).CurrentValues.SetValues(temp);

Hiç yorum yok:

Yorum Gönder