簡體   English   中英

無法刪除帶有子實體的實體

[英]Cannot delete entity with child entities

在刪除具有關聯記錄的記錄時遇到問題。

這是一個非常簡單的設置:“組件”可以有一個或多個(或沒有!)“ 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”實體方面都設置為“已刪除”,在保存更改時都會出現相同的錯誤。

Cascadedelete啟用了這種關系,如果我在SQL企業管理器中刪除了一個組件,則將立即刪除所有子組件進程,以使其正常工作。

SQL錯誤似乎與您聲稱已為級聯刪除設置db關系的說法不一致……盡管我無法解釋您如何在SQL Server Management Studio中擺脫它。

也許您可以跟蹤數據庫調用的順序?

我確實知道您嘗試刪除子進程的麻煩是什么:在迭代時要更改editComponent.processes數組! 那是不可以的。

每次調用setDeleted ,Breeze都會從父editComponent.processes數組中刪除該Process實體。 當迭代器循環返回時,它將跳過下一個process

在對其內容調用setDeleted之前,需要制作該數組的副本 這樣的事情應該做到:

// copy with slice()
var processes = editComponent().processes().slice(); 
// iterate over the copy
processes.forEach(function (p) {
    // Breeze removes each `p` from its parent editComponent.processes array
    p.entityAspect.setDeleted(); //marks deleted but won't persist until saveChanges is called
});

expect(editComponent().processes().length).to.equal(0);

HTH

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM