Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you were using POCO entities with <code>ObjectContext</code> it worked indeed. But not because EF's change tracking worked differently than with <code>DbContext</code> but because the POCO entities generated by the EF 4 T4 templates contained "relationship fixup" methods.</p> <p>Basically the property setter for the line <code>parentObject = myParentObject</code> wasn't only an object assignment but the setter included a call to a method that in the end exactly did what you are doing manually now, namely: <code>myParentObject.childObjects.Add(myChildObject)</code>. At this point the rule <em>"You can add a new entity to the context by hooking it up to another entity that is already being tracked"</em> applies and <code>myChildObject</code> gets added to the context and inserted into the database.</p> <p>For the T4 templates that generate POCO entities for <code>DbContext</code> those fixup methods have been removed because they were causing trouble in other scenarios. Especially when lazy loading is involved your reference assignment and the automatic call of <code>myParentObject.childObjects...</code> in the property setter would trigger lazy loading on the collection and load all <code>childObjects</code> first that are already stored for <code>myParentObject</code> before the new child is added to the collection. If those are thousands this is a huge unnecessary overhead, performance gets disastrous and the reason for a suddenly bad performance (just because you assigned a single reference property) isn't easy to detect if you are not aware of the fixup methods that run behind the scenes.</p> <p><a href="https://stackoverflow.com/questions/6643875/entity-framework-inserts-row-without-me-calling-addtoobject">Here</a> and <a href="https://stackoverflow.com/questions/6378063/entity-framework-4-very-slow-linking-an-order-to-customer-who-has-10-000-orders">here</a> and <a href="https://stackoverflow.com/questions/13751176/entity-framework-runs-through-all-records-60-000-when-updating-property-for-o">here</a> and <a href="https://stackoverflow.com/questions/6723784/should-i-rip-out-the-association-fixup-code-from-my-entity-framework-t4-template">here</a> are examples about the confusion that relationship fixup methods were causing.</p> <p>You <em>could</em> modify the T4 templates and add relationship fixup methods again - or if you are using Code-First just write them by hand in your entity classes - to get the old behaviour back. But this might be more complex than and at least as much work as changing your existing code the way you've outlined in your last code snippet - which I would certainly prefer over having those bothersome fixup methods back.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload