Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to cascade delete only on join table using Entity Framework Code First Fluent API
    primarykey
    data
    text
    <p>I'm trying to map an existing database using Entity Framework Code First Fluent API. I'm struggling on a situation with a many-to-many relationship and cascade delete.</p> <p>Here's my two POCO classes:</p> <pre><code>public class Foo { public int FooId; public string FooDescription; public IList&lt;Bar&gt; Bars; } public class Bar { public int BarId; public string BarDescription; } </code></pre> <p>For now, I only have a navigation property from <code>Foo</code> to <code>Bar</code>. That's because in my real scenario, it doesn't make sense for anyone to go from <code>Bar</code> to <code>Foo</code>. The relationship from <code>Foo</code> to <code>Bar</code> is actually 0,n which means one instance of <code>Foo</code> do not require instances of <code>Bar</code>.</p> <p>My existing database has a join table between those two tables. This table looks like this:</p> <pre><code>create table Foo_Bar( FooId int not null, BarId int not null ); </code></pre> <p>Because I prefer to not create such a POCO class in my project, I mapped the table like this:</p> <pre><code>modelBuilder.Entity&lt;Foo&gt;() .HasMany(t =&gt; t.Bars) .WithMany() .Map(m =&gt; { m.ToTable("Foo_Bar"); m.MapLeftKey("FooId"); m.MapRightKey("BarId"); }); </code></pre> <p>That works perfectly for inserts. Entity insert on Foo and then on the existing <code>Foo_Bar</code> table.</p> <p>But when I delete a <code>Foo</code>, I would like to delete the records on the join table but not on the child table. Meaning I want to delete records on <code>Foo_Bar</code> but not to extend this cascade to <code>Bar</code>.</p> <p>Is such a mapping possible? I should mention that on my context I removed both <code>OneToManyCascadeDeleteConvention</code> and <code>ManyToManyCascadeDeleteConvention</code> because 99% of the times I won't be using cascade delete. This is one specific situation.</p> <p>By tracing the SQL generated by Entity Framework, I also noticed that if I retrieve a <code>Foo</code> instance and set all Bars inside it to EntityState.Deleted, EF will delete both records in <code>Foo_Bar</code> and <code>Bar</code>. One last thing I should mention is that I'm working in a disconnected scenario.</p> <p>I tried to search this over but it seems I'm not using the right keywords as I could not find anyone in such a situation. At this point the only solution I see is to <code>Map</code> a <code>FooBar</code> class but I'd hope there's a way to do this.</p> <p><strong>--Edit</strong></p> <p>It appears that my approach to delete was wrong at first. When I tried deleting I wasn't passing the instance of <code>Foo</code> with its list of <code>Bar</code> filled. After that change, Entity Framwork creates a delete statement for every <code>Bar</code> on the list. I still have problems with updating like adding and removing items at the same time. Will do more tests and post here.</p> <p>Thanks in advance.</p>
    singulars
    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.
    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