This has been bugging me for some time now. My problem is the following. I have a instance of type X that is called x. X is an entity class that is generated by Entity Framework.
Inside my method I receive x from a web service, so it's not attached to my object context, but I know that an element with the same primary key exist in the database already, so I want to update it. I added the following body to the method, and it worked fine:
objectContext.Xs.Attach(x);
objectContext.ObjectStateManager.ChangeObjectState(x, EntityState.Modified);
objectContext.SaveChanges();
So far all good. However my objectContext is not created by this method, and suddenly this code stopped working and threw a InvalidOperationException
with the following text:
An object with the same key already exists in the ObjectStateManager. The ObjectStateManager cannot track multiple objects with the same key.
This happened because sometime before this code was run, the following code was executed:
objectContext.Xs.ToList();
This read all the current X objects from the database, and thereby added them to the ObjectStateManager.
I then changed by code to this:
objectContext.Xs.ApplyCurrentValues(x);
objectContext.SaveChanges();
This worked fine, but if I remove the objectContext.Xs.ToList();
line again, I get another InvalidOperationException
with the following text:
An object with a key that matches the key of the supplied object could not be found in the ObjectStateManager. Verify that the key values of the supplied object match the key values of the object to which changes must be applied.
So now I got the opposite problem. This method does not work because it's not tracked by the ObjectStateManager.
In addition I haven't found any reliable method of determining if my x is already tracked or not, so currently I have added the code like this:
try
{
objectContext.Xs.Attach(x);
objectContext.ObjectStateManager.ChangeObjectState(x, EntityState.Modified);
objectContext.SaveChanges();
}
catch(InvalidOperationException)
{
objectContext.Xs.ApplyCurrentValues(x);
}
objectContext.SaveChanges();
This means that I am using exception handling as normal program flow (it's hardly exceptional that the element is already tracked by the ObjectStateManager), and I also have the problem that I might catch another unrelated InvalidOperationException
that I should have handled in another way.
So my question is. Does anyone know a good solution for this, or at least a proper way to check if x is tracked by the ObjectStateManager so I can choose one of the method based on that instead of a try/catch?