刪除具有關聯記錄的記錄時遇到問題。無法刪除包含子實體的實體
這是一個非常簡單的設置:「組件」可以有一個或多個(或沒有!)「componentProcesses」。如果刪除沒有進程的組件,則不會出現錯誤,並且組件已成功從數據庫中刪除。如果我刪除與處理的組件,然後我收到以下消息上調用manager.saveChanges():
The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_dbo.componentProcesses_dbo.components_ComponentID". The conflict occurred in database "mydb", table "dbo.components", column 'Id'
的模式基本上是:
Public Class component
Public Property Id() As Integer
...irrelevant fields removed....
Public Overridable Property Processes() As ICollection(Of componentProcess)
End Class
Public Class componentProcess
Public Property Id() As Integer
...irrelevant fields removed....
Public Property ComponentID() As Integer 'process belongs to component
Public Property ProcessId() As Integer 'links to specific process
Public Overridable Property Component() As component
Public Overridable Property Process() As process
End Class
我不會顯示「組件」和「過程」模型,因爲它們只是簡單的基於id的靜態表。
當用戶刪除一個組件,代碼:
//editComponent is a ko.observable containing currently-being-edited component
//editComponent().processes().forEach(function (p) {
// p.entityAspect.setDeleted(); //sets to deleted but won't persist if save isn't called
//editComponent().processes.remove(function (_process) {
// return _process.id() == p.id()
//});
//});
editComponent().entityAspect.setDeleted(); //sets to deleted but won't persist if save isn't called
editComponent(null); //empty observable to trigger screen UI updates
正如你可以從上面的代碼看,我已經和註釋掉各行,看看它是否會影響試驗結果,但它沒有。無論是否將每個孩子的「componentProcess」entityaspect設置爲「已刪除」,我在保存更改時都會遇到同樣的錯誤。
Cascadedelete處於這種關係,如果我刪除SQL企業管理器中的組件,所有子組件進程都會立即刪除,以便正常工作。
忘記關於這個問題,我被分心。只是測試應用程序,並摔倒了這個錯誤,並記得我問過這個問題。示例代碼完美工作。再次感謝病房,我明白你的意思。 – TheMook 2014-10-29 11:33:35