2015-07-20 65 views
0

我有兩個模型[DOCTOR] & [CONTACTS],它們通過使用Entityframework 6.0的多對多關係相關聯。我可以添加一個醫生找到以下內容:
DM是醫生實體周圍的包裝類,以便我可以使用onpropertychange綁定到它。EF6多對多刪除不起作用

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(); 
} 

回答

1

DM.DOCTOR未被您的上下文跟蹤。 SaveChanges之前,請致電:

context.Doctors.Attach(DM.DOCTOR);