.net - Entity Framework AddTo function inconsistency? -


Describe the behavior I am getting:

  • Load the user from the database: This means that the user is related to the context
  • Create a new Object C:
    • C tempC = new C ();
    • tempC.User = previously loaded users;
    • Context. TempC;
    • The last line throws an exception because the object was added to the context when the property user was set.

But if I do the following:

  • Load a user from the database: This means that the user can Is linked
  • Create a new Object C:
    • C tempC = new C ();
    • tempC.User = previously loaded user;
    • Context.SaveChange ();
    • Create a new Object e, whose purpose is to relate to Object C.
      • e tempE = new E ();
      • tempE c = previously created c;
      • Context. ADOSET (Tempee);

    Enter an exception not I was expecting an exception because since then it is related to the reference, which is That should be similar to the first example. But this is not why, and what can I do for some fellowship?

    Before planning to add to the set, I am planning to examine the state of the object (Entitestate == different), but I thought I should do something to get started with disturbances.

assuming that users and c In, you may want to use a different syntax to add C to parent user . tempC.User = instead of the first user loaded; You might want to add tempC as the child of the user . Here's what I mean:

  C tempC = new C (); (User created before) c. Add (tmpsea); Context.AddToCSet (tempC);  

Comments