Richard Banks
Richard Banks

Reputation: 2983

Using Entity Framework 6 to delete from a junction table but leave the record in the lookup table

I have 2 tables, 1 main table, 1 junction table and 1 lookup table. It's a 1-to-many-to-1 relationship. I'm looking for a way to delete all the entries from the junction table only, without removing the lookup value.

An example of my tables are below

Record table

Junction table

Lookups table

Currently I'm passing in an updated model and I'm trying to delete all the junction records before reading the new chosen values. So far everything I've tried deletes out the record from the lookup table as well.

 foreach (var lookup in recordFromDB.Lookups.ToList()) {
     _context.Lookups.Remove(DesignSoftware);
 }

Upvotes: 2

Views: 431

Answers (1)

marc_s
marc_s

Reputation: 754868

Try this - just using the .Clear() method on the navigation property should remove any navigation (a.k.a. "junction") entries - but not the actual Lookups entries:

recordFromDB.Lookups.Clear();
YourContext.SaveChanges();

Upvotes: 4

Related Questions