如何在实体框架中级联删除?

Bic*_*ick 4 c# sql entity-framework entity-framework-4

我的模型中有两个对象

汽车和汽车零件

与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.
Run Code Online (Sandbox Code Playgroud)

我认为它首先尝试删除汽车对象然后再删除汽车零件.
由于外键,这是不可能的.

我该如何处理?
我想,显然先删除carPart然后再删除汽车.
谢谢.

Roe*_*elF 6

您需要告诉数据库您要在删除时级联,然后实体框架将执行您期望的操作.如果转到RelationshipsSQL Server Management Studio中的表的屏幕,则可以更改FK行为:

Sql Server Management Studio中的级联删除