EF6 多对多删除不工作
EF6 Many to Many Delete Not working
我有两个模型 [DOCTOR] 和 [CONTACTS],它们使用 Entityframework 6.0 通过多对多关系关联。我可以通过以下方式添加医生查找:
DM 是医生实体的包装器 class,因此我可以使用 onpropertychange 绑定到它。
using (var context = new RxStoreEntities())
{
contact C = context.contacts.First(i => i.LoginID == loginID);
C.Doctors1.Add(DM.DOCTOR);
context.SaveChanges();
}
当我尝试删除它时,它不会删除。我什至检查了 SQL Profiler,但我没有看到我应该看到的删除 SQL 功能。删除代码如下:
using (var context = new RxStoreEntities())
{
contact C = context.contacts.First(i => i.LoginID == loginID);
C.Doctors1.Remove(DM.DOCTOR);
context.SaveChanges();
}
DM.DOCTOR 未被您的上下文跟踪。在 SaveChanges 之前,调用:
context.Doctors.Attach(DM.DOCTOR);
我有两个模型 [DOCTOR] 和 [CONTACTS],它们使用 Entityframework 6.0 通过多对多关系关联。我可以通过以下方式添加医生查找:
DM 是医生实体的包装器 class,因此我可以使用 onpropertychange 绑定到它。
using (var context = new RxStoreEntities())
{
contact C = context.contacts.First(i => i.LoginID == loginID);
C.Doctors1.Add(DM.DOCTOR);
context.SaveChanges();
}
当我尝试删除它时,它不会删除。我什至检查了 SQL Profiler,但我没有看到我应该看到的删除 SQL 功能。删除代码如下:
using (var context = new RxStoreEntities())
{
contact C = context.contacts.First(i => i.LoginID == loginID);
C.Doctors1.Remove(DM.DOCTOR);
context.SaveChanges();
}
DM.DOCTOR 未被您的上下文跟踪。在 SaveChanges 之前,调用:
context.Doctors.Attach(DM.DOCTOR);