Delete connection table entry in Entity Framework Code First (M: M)

How to delete an entry in the connection table in Entity Framework 5?

When reverse engineering my DataContext, the Entity Framework seemed to recognize my join table and automatically add collections to my models to represent the M: M relationship. This is great when adding elements, as I can just build my entire Entity and everything will work out right. Perfect.

However, I do not understand how to withdraw a relationship. For example, an Activity may have several contacts associated with it, and this is due to the use of the connection table (dbo.ActivityContacts), which consists of columns:

  • ActivityID
  • Contactid

Both my Activity and Contact models were updated by EF using Collections to represent another. For example, my Activity model is as follows:

public class Activity { public int ActivityID { get; set; } public string Subject { get; set; } public virtual ICollection<Contacts> Contacts { get; set; } } 

In a non-EF environment, I just delete the entry from the connection table and move on with my day. However, it seems that I cannot access the navigation table directly using EF, so I got a little confused about how to delete the record (relationships).

How to delete an entry from the connection table in the Entity Framework?

+4
source share
4 answers

Agree with @Chris.

Another solution:

 context.Entry(activity).State = EntityState.Deleted; 
+1
source

Entity Framework should delete the entry for you if you delete the associated object from either side of the relationship.

Assuming you got this instance of Activity from your context and want to remove a specific Contact with a known identifier:

 unwantedContact = context.Contacts.Find(contactID); myActivity.Contacts.Remove(unwantedContact); context.SaveChanges(); 

You should delete the entry in your connection table if I am not out of date.

+2
source

ali golshani did a good job providing a solution. Let me try to expand it a little more. In my scenario, I have a list with inline checkboxes per item. I want to be able to add / remove marked items.

The 'dto' object specified below is sent by the client. It checks the selected state for each item in the list. If anyone knows of any way to improve this, please leave a review.

 file_appender selectedAppender = context.file_appender.Find(dto.Id); int[] ids = dto.Loggers.Where(x => !x.Selected).Select(x => x.Id).ToArray(); var loggers_to_delete = selectedAppender.logger.Where(x => ids.Contains(x.id)); loggers_to_delete.ToList().ForEach(x => { selectedAppender.logger.Remove(x); }); ids = dto.Loggers.Where(x => x.Selected).Select(x => x.Id).ToArray(); var loggers_to_add = context.logger.Where(x => ids.Contains(x.id)); loggers_to_add.ToList().ForEach(x => { selectedAppender.logger.Add(x); }); 
+1
source
 contact_to_delete = context.Contacts.Find(contactID); selected_activity = context.Activity.Find(ActivityID); context.Entry(selected_activity).Collection("Activity").Load(); selected_activity.Contacts.Remove(contact_to_delete); db.SaveChanges(); 
0
source

Source: https://habr.com/ru/post/1490396/


All Articles