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.