Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3690550
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T04:02:52+00:00 2026-05-19T04:02:52+00:00

I ran into this problem using EF4 and a self-referential table (implementing an adjacency

  • 0

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?!
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T04:02:52+00:00Added an answer on May 19, 2026 at 4:02 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today, I ran into this weird problem with a user using Mac OS X.
I'm writing a C parser using PLY, and recently ran into a problem. This
I am working on an image upload script and ran into this problem. Using
I deployed an ASP.NET MVC app using MSDeploy and ran into this problem i
This originally was a problem I ran into at work, but is now something
I know this question has been asked before, but I ran into a problem.
I have ran into this problem a few times and I am wondering what
Attempting to cutover our EF4 solution to EF CTP5, and ran into a problem.
I ran into this problem while developing in Objective-C for iOS, but this should
I ran into the following problem using spring-security 3.0.3 with jersey 1.2: when i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.