c# - 如何在 Entity Framework 中级联删除?

标签 c# sql entity-framework entity-framework-4

我的模型中有两个对象

汽车和汽车零件

具有 1:n 关系。

我想删除级联的实体车。 当我删除时出现以下异常:

 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,然后再删除汽车。
谢谢。

最佳答案

您需要告诉数据库您想要级联删除,然后 Entity Framework 将执行您期望的操作。如果转到 SQL Server Management Studio 中表的 Relationships 屏幕,则可以更改 FK 行为:

Cascade delete in Sql Server Management Studio

关于c# - 如何在 Entity Framework 中级联删除?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13511005/

相关文章:

sql - 如何找到关系的 COUNT 结果的 MAX

SQL 数据库父/子递归

mysql - mysql 中的多个 DELETE 不适用于别名

c# - Fluent Nhibernate ManyToMany 集合 - 不保存关联

c# - LINQ 到 SQL : Updating without Refresh when “UpdateCheck = Never”

c# - Entity Framework 、存储库模式和 let 语句

c# - Entity Framework 6 - 计时查询

asp.net-mvc - edmx 重建会创建一个重复的设计器 cs 文件

C# 实用程序类 - 设置为一般用途并编译 w/o Main

c# - 有没有办法获取对嵌入式资源文件的字符串路径引用?