简体   繁体   中英

EF6 Many to Many Delete Not working

I have two models [DOCTOR] & [CONTACTS] that are associated via a many-to-many relationship using Entityframework 6.0. I can add a doctor find with the following:
DM is a wrapper class around the doctor entity so that I can bind to it using onpropertychange.

using (var context = new RxStoreEntities())
{
   contact C = context.contacts.First(i => i.LoginID == loginID);
   C.Doctors1.Add(DM.DOCTOR);
   context.SaveChanges();
}

When I do the following to try to delete it, it will not delete. I even checked SQL Profiler and I am not seeing the delete SQL function like I should be seeing. The code for the delete is as follows:

using (var context = new RxStoreEntities())
{
      contact C = context.contacts.First(i => i.LoginID == loginID);
      C.Doctors1.Remove(DM.DOCTOR);
      context.SaveChanges();
}

DM.DOCTOR isn't tracked by your context. Before SaveChanges, call:

context.Doctors.Attach(DM.DOCTOR);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM