AddObject or Attach should not add or attach whole object graph
AddObject and Attach methods should have optional parameter to specify, if I want to add or attach whole object graph or the signle object only.
If I have two related objects, which are detached) and one is new and the other existing, it is very tricky to put them in ObjectContext.
We implemented this in EF Core. However we decided that DbEntityEntry.State would be the way to affect single objects.
James D. Schwarzmeier commented
More generally, we should have the ability when setting the state of any DbEntityEntry or ObjectStateEntry, to specify whether or not it should apply to ONLY that entry or to the entire object graph. There are scenarios where one might want the automatic SQL generation and automatic object population benefits of Entity Framework, but still want manual control over exactly which records get updated. Imagine in a disconnected, n-tier scenario, where an object A might hold on to a PARTIALLY populated object B (perhaps for reasons of HTTP request size). I might want to set object A's state to modified and save to the DB it without trying to also save anything internal to object B. I also would not want it to modify the state of any of my objects (like it does today when calling detach).
If I choose to NOT have Entity automatically save the full object graph, I would expect it to throw an exception if I try to call SaveChanges and the end result violates the database's referential integrity (e.g. row A has a foreign key to a non-existant row B).
Ladislav Mrnka commented
That would simply mean that all relations will be removed from your object (navigation properties will be set to null) in the same way as Detach currently works.