Question

I ran into this problem using EF4 and a self-referential table (implementing an adjacency list hierarchy).
NOTE: not a many-to-many reference, just a one-to-many on a single table.

Attempts to resolve an intermittent InvalidOperationException ("...The ObjectContext might be in an inconsistent state...") using Context.Refresh fail due to an apparent bug in EF4.

I saw, from following Shimmy's connect.microsoft.com link, on the aforementioned post, that the bug is still outstanding.
Can anyone recommend a workaround?
What do you do if your database and Entity Framework get out of sync?

EDIT
Some more facts that may help:

  1. When I get the InvalidOperationException and the message says "The changes to the database were committed successfully...", it is not true. They weren't. I tried to change an object's ParentId from 1 to null (ParentId of type int?).
  2. My object's ParentId attribute is correctly changed to the expected value (null). I call Context.SaveChanges(). Then, the exception is thrown. I check the DB, and the value has not been updated. In this case, ParentId is still 1 in the database.
  3. Inside the catch, if I try to requery the object via
    myObj = Context.MyObjects.SingleOrDefault(o => o.Id == id),
    the object's ParentId remains the same. It does not get updated by the (incorrect) database value!
    Now I think, Okay, that seems weird, but maybe if I save again the db will be corrected.
  4. Calling a subsequent Context.SaveChanges() from inside the catch still does not update the database. (But this time, an exception is not thrown.)
  5. If I make a new call to my SetParent method,
    myObj = Context.MyObjects.SingleOrDefault(o => o.Id == id),
    correcly populates the object's ParentId parameter to 1, the value from the database.

Additionally, for giggles, I set the object's ParentId to it's own Id, instead of null, to denote parentlessness. This worked fine and did not cause the InvalidOperationException. But, it's a PITA for other reasons. E.g., the object reports having itself as an extra child.

So, the questions are:

  • What is it about trying to set my self-referential int? ParentId to null that causes an exception?
  • Why does the db not get updated before the exception?
  • And why, inside the catch, can't I resync?!
Was it helpful?

Solution

Requery the object from the database through the context...

EDIT - in response to your update, if you submit changes for an object within a context, an an error happens, using the same context most likely is the problem. Try recreating the context in the catch to requery and reupdate, and see if that works any better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top