簡體   English   中英

如何在實體框架中級聯刪除?

[英]How to cascade delete in entity framework?

我的模型中有兩個對象

汽車和汽車零件

與1:n的關系。

我想刪除cascade實體車。 當我刪除時,我得到以下異常:

 The operation failed: The relationship could not be changed because one or 
 more of the foreign-key properties is non-nullable. When a change is made 
 to a relationship, the related foreign-key property is set to a null value. 
 If the foreign-key does not support null values, a new relationship must 
 be defined, the foreign-key property must be assigned another non-null value, 
 or the unrelated object must be deleted.

我認為它首先嘗試刪除汽車對象然后再刪除汽車零件。
由於外鍵,這是不可能的。

我該如何處理?
我想,顯然先刪除carPart然后再刪除汽車。
謝謝。

您需要告訴數據庫您要在刪除時級聯,然后實體框架將執行您期望的操作。 如果轉到SQL Server Management Studio中的表的“ Relationships屏幕,則可以更改FK行為:

Sql Server Management Studio中的級聯刪除

如果要刪除Cascade,請在數據庫級別設置級聯刪除。 您收到錯誤,因為SQL不允許刪除。

您不必在Entity Framework中執行此操作。

暫無
暫無

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

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