我有一個與另一個對象具有多對多關係的對象。我正在嘗試編寫一個更新語句,該語句不會導致必須首先從多對多表中刪除所有記錄。多對多實體框架更新
我的數據是: StoredProcedure的 - StoredProcedureId,名稱 參數 - 參數標識,名稱 StoredProcedure_Parameter - StoredProcedureId,參數標識,訂單
我有用於更新存儲procedured對象(添加/刪除參數或改變的UI參數的順序)。
當我保存,我結束了在:
var storedProcedure = context.Sprocs.FirstOrDefault(s => s.SprocID == sproc.StoredProcedureId);
if (storedProcedure == null)
{
//do something like throw an exception
} else
{
storedProcedure.Name = sproc.Name;
//resolve Parameters many to many here
//remove all Params that are not in sproc.Params
//Add any params that are in sproc.Params but not in storedProcedure.Params
//Update the Order number for any that are in both
}
我知道我可以簡單地在桌子上調用.Clear(),然後重新插入所有的值與當前狀態(確保所有參數被用戶界面刪除後,新的添加,並更新訂單改變)。不過,我覺得必須有更好的方式來做到這一點。使用EF進行多對多更新通常可以通過刪除所有元素並重新插入來解決?