I am using the following method of trying to update an object using entity framework:
public static void UpdateItem(Item updatedObject) {
using (var context = new DbContext())
{
context.MyObjectsPropertys.Attach(updatedObject);
context.Entry(updatedObject).State = EntityState.Modified;
context.SaveChanges();
}
}
I get an error on the attach:
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
If I try to change the Attach call to an Add call (object with same key already exists objectstatemanager) I get:
Cannot insert duplicate key in object 'database.Table'. The duplicate key value is (AttributeValue). The statement has been terminated.
I have found lots of questions regarding this error, but none of the answers work in my situation:
I tried deleting the "Attach" statement as well, and just doing the state change as suggested in Error multiple objects with the same key DbContext, but that didn't work either.
The most frustrating thing to me on this is that this is the exact same sequence of statements that successfully updates other things in the solution.