Note that there are some explanatory texts on larger screens.

plurals
  1. POCascading deletes with Entity Framework - Related entities deleted by EF
    primarykey
    data
    text
    <p>I have an issue with deletion in Entity Framework. In short, EF explicitly tries to delete an entity from the database even though I've explcitly configured EF to use cascading deletes in the database.</p> <p><strong>My design:</strong></p> <p>I have three entity types, <code>MainEntity</code>, <code>EntityTypeA</code> and <code>EntityTypeB</code>. EF has been configured to use cascade deletion when deleting <code>EntityTypeA</code> and <code>EntityTypeB</code>. In other words, if I delete an instance of <code>MainEntity</code>, I want all related <code>EntityTypeA</code> and <code>EntityTypeB</code> instances to be deleted as well. I never delete <code>EntityTypeA</code> or <code>EntityTypeB</code> without also deleting their parent.</p> <p>My problem is that EF explictly issues a <code>DELETE</code> statement for <code>EntityTypeA</code>, which causes my application to crash.</p> <p>This is what my model look like:</p> <p><img src="https://i.stack.imgur.com/KC43E.png"/></p> <p>The relations have the following non-default config:</p> <ul> <li><code>MainEntity -&gt; EntityTypeA OnDelete: Cascade</code></li> <li><code>MainEntity -&gt; EntityTypeB OnDelete: Cascade</code></li> </ul> <p>The relation <code>EntityTypeA -&gt; EntityTypeB</code> has <code>OnDelete: None</code></p> <p><strong>Database contents</strong></p> <pre><code>INSERT INTO MainEntities (Name) values ('Test') insert into EntityTypeA (MainEntityID) VALUES (1) insert into EntityTypeB (MainEntityID, EntityTypeAId) VALUES (1, 1) insert into EntityTypeB (MainEntityID, EntityTypeAId) VALUES (1, 1) </code></pre> <p><strong>My code:</strong></p> <pre><code>class Program { static void Main(string[] args) { var context = new Model1Container(); var mainEntity = context.MainEntities.Include("EntityTypeA").SingleOrDefault(); context.DeleteObject(mainEntity); context.SaveChanges(); } } </code></pre> <p><strong>What happens</strong></p> <p>When I call SaveChanges, Entity Framework executes the following in the database:</p> <pre><code>exec sp_executesql N'delete [dbo].[EntityTypeA] where ([Id] = @0)',N'@0 int',@0=1 </code></pre> <p>This causes an foreign key violation, because there are items in EntityTypeB's table referencing EntityTypeA instances.</p> <p><strong>Question</strong></p> <p>Why does Entity Framework issue an explicit delete for the instance of EntityTypeA even though I've configured Entity Framework to use cascading deletes? If I remove the Include("EntityTypeA") it starts working again.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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