删除一个记录并将外键设置为null时,是否支持实体框架? [英] Does Entity Framework support when deleted a record and set the foreign key to null?

查看:191
本文介绍了删除一个记录并将外键设置为null时,是否支持实体框架?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的模型:

public class Account
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int? ParentId { get; set; }
    public Account Parent { get; set; }
}

我添加以下配置:

    this.HasOptional(item => item.Parent)
        .WithMany()
        .HasForeignKey(item => item.ParentId)
        .WillCascadeOnDelete(true);

然后我收到以下错误消息:

and then I got following error message:

程序集初始化方法
UnitTest.Biz.Accounting.TestInitializer.Init抛出异常。
System.Data.SqlClient.SqlException:
System.Data.SqlClient.SqlException:介绍FOREIGN KEY约束
'FK_dbo.acct_Account_dbo.acct_Account_ParentId'在表
'acct_Account'可能导致循环或多个级联路径。指定ON
DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY
约束。无法创建约束。

Assembly Initialization method UnitTest.Biz.Accounting.TestInitializer.Init threw exception. System.Data.SqlClient.SqlException: System.Data.SqlClient.SqlException: Introducing FOREIGN KEY constraint 'FK_dbo.acct_Account_dbo.acct_Account_ParentId' on table 'acct_Account' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint.

我已经阅读了EF的文档,但我不明白我的代码在哪里错了。

I had read the documents of the EF, but I don't understand where is wrong in my code...


如果依赖实体上的外键为空,Code First将在关系上设置级联删除,当主体
被删除时,外键将被设置为null
http://msdn.microsoft.com/en-us/data/jj591620#CascadeDelete


推荐答案

自引用表

SQL服务器不允许自引用表具有级联删除。 p>

SQL server doesn't allow self referencing table to have cascading delete.


如您所见,SQL Server注意到您的级联操作是
循环,并且不允许此类型的级联。这是真的,而不是
仅用于自引用表,而且对于级联操作具有
潜力为周期性的表之间的任何
关系链。

As you can see, SQL Server noticed that your cascade operation is cyclic, and it does not allow this type of cascading. This is true not only for a self-referencing table, but also for any chain of relationships between tables in which the cascading operations have a potential to be cyclical.

来源

这就是为什么在EF中你不能在自引用实体上设置级联删除。

That's why in EF you can't set cascading delete on self referencing entity.

.WillCascadeOnDelete(true); // Not allowed.

如果依赖实体上的外键为空,Code First将
没有在关系上设置级联删除

是关于默认约定。如果您有以下代码,

is about the default convention of EF which you can override by fluent api. If you have following code,

public int? ParentId { get; set; } // <--- nullable
public Account Parent { get; set; }

默认情况下,EF将标记级联删除为false,即使您没有配置它 WillCascadeOnDelete(false),这实际上意味着数据库中的外键级联删除将被设置为 ON DELETE NO ACTION

EF will mark the cascading delete as false by default, even if you don't configure it with WillCascadeOnDelete(false), which actually means that the foreign key cascading delete in the database will be set as ON DELETE NO ACTION.

目前,EF不支持 ON DELETE SET NULL ,除非您有自定义查询来删除和重新 - 添加约束,检查这篇文章

And currently EF doesn't support ON DELETE SET NULL unless you have custom query to drop and re-add the constraint, check this post.

当主体被删除时,外键将被设置为null。

这已在这篇文章,这意味着只有在孩子被加载的情况下

This has been explained in this post which means only if the children are loaded into the context.

这篇关于删除一个记录并将外键设置为null时,是否支持实体框架?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆