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

  • Home
  • SEARCH
  • 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 853637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:47:32+00:00 2026-05-15T07:47:32+00:00

I have come across what appears to be very peculiar behaviour using entity framework

  • 0

I have come across what appears to be very peculiar behaviour using entity framework 4.0.

I have a User entity, which has a Company (A Company has many Users).

If I do this, everything works as expected and I get a nice shiny new user in the database:

var company = _model.Companies.First();
company.Users.Add(new User(1, "John", "Smith"));
_model.SaveChanges();

However, if I do this, then I get nothing new in the database, and no exceptions thrown:

var existingUser = _model.Users.First();
var company = existingUser.Company;
company.Users.Add(new User(1, "John", "Smith"));
_model.SaveChanges();

So it appears that if I add a User to the Company that is pulled directly from the model, then everything works fine. However if the User is added to a Company that is pulled as a navigation property of another object, then it doesn’t work.

Can someone tell me if this is expected behaviour, or if there is something I can do to make it so that it is?

Thanks!

Edit

To clarify what I mean by “it doesn’t work”;

  • Stepping through the code shows the execution pointer going past all lines without throwing an exception.
  • No new row is added to the database
  • If I check company.Users in quick watch, the new user has indeed been added to the company – it’s just not being saved to the database.

I’ve done a little more playing, and it seems that if I do this, I get an exception:

var existingUser = _model.Users.First();
var company = existingUser.Company;
_model.ObjectStateManager.GetObjectStateEntry(company);

The exception is:

System.InvalidOperationException: The
ObjectStateManager does not contain an
ObjectStateEntry with a reference to
an object of type ‘ABC.DEF.Company’.

Doing that for the first (working) scenario provides me with an ObjectStateEntry back without throwing an exception.

  • 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-15T07:47:33+00:00Added an answer on May 15, 2026 at 7:47 am

    I figured it out…

    I simplified the code for the purpose of this question, but when I drilled into it, I realised that the _model instance that I was getting the user from was different to the one that was saving the company. So really, what I was actually doing was something more like this:

    var existingUser = _modelFromUserDAL.Users.First();
    var company = existingUser.Company;
    company.Users.Add(new User(1, "John", "Smith"));
    _modelFromCompanyDAL.SaveChanges();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.