Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://weblogs.asp.net/zeeshanhirani/archive/2010/07/23/removing-entity-from-a-related-collection.aspx" rel="nofollow noreferrer">http://weblogs.asp.net/zeeshanhirani/archive/2010/07/23/removing-entity-from-a-related-collection.aspx</a> explains exactly what happened to you.</p> <hr> <p>Assuming you have a class design something like this:</p> <p><img src="https://i.stack.imgur.com/IuTof.png" alt="sample class design"></p> <p>Entity Framework will generate the required foreign key columns and add <code>NOT NULL</code> constraints to them because all Recipes will always be associated with exactly one ControlUnit.</p> <p>So at runtime you will have objects similar to the following layout:</p> <p><img src="https://i.stack.imgur.com/BCB7S.png" alt="object diagram at runtime"></p> <p>Now your code comes into play and deleted the relationship between the Recipe objects and their ControlUnit:</p> <p><img src="https://i.stack.imgur.com/bEDR0.png" alt="objects with deleted relationships"></p> <p>Trying to save at this moment, the database does not have a ControlUnit ID to put into the foreign key <code>NOT NULL</code> column. The current object state violates the class diagram above and cannot be saved to a database layout that has been generated under the assumption that every Recipe is associated with one ControlUnit. This is why the database refuses to save the changes and you see the exception.</p> <p>This also explains why it works when you uncomment the line deleting the entity: The entity is removed from the database along with its relationship, so no constraints are violated, therefore no exception.</p> <p><strong>"But I set <code>ON DELETE CASCADE</code> on the relationship..."</strong></p> <p>Yes, but that is only triggered on deletion of the object, not on deletion of the relationship. With <code>ON DELETE CASCADE</code> set, this should work:</p> <pre><code>controlUnitRepository.DeleteObject(_controlUnit); // deletes the ControlUnit and all associated Recipe entities </code></pre> <p>If you want to trigger deletion of the Recipe entities on deletion of their relationship with ControlUnit, your relationship should not be a simple association but rather a composition:</p> <p><img src="https://i.stack.imgur.com/6TJEA.png" alt="updated class diagram with composition"></p> <p>EF does not support this natively, but you can emulate the behaviour using identifying relationships. Once an entity is in an identifying relationship to a parent entity and that relationship is removed, the entity is removed as well. It seems this was your intention from the start. For more information on identifying relationship, see <a href="https://stackoverflow.com/questions/3710191/implementing-identifying-relationships-with-ef4">Implementing identifying relationships with EF4</a> where I implemented identifying relationships with EF4 and have linked to more reading material.</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