引入FOREIGN KEY约束可能会导致循环或多个级联路径? [英] Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths?

查看:68
本文介绍了引入FOREIGN KEY约束可能会导致循环或多个级联路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从实体框架代码优先生成数据库时出现此错误。

I am getting this error when generate Database from Entity Framework Code First.

我没有看到脚本的任何问题:

I didn´t see any problem with script:

alter table [dbo].[Votes] add constraint [Post_Votes] foreign key ([Post_Id]) references [dbo].[Posts]([Id]) on delete cascade;

alter table [dbo].[Votes] add constraint [User_Votes] foreign key ([Voter_Id]) references [dbo].[Users]([Id]) on delete cascade;

每个引用都指向不同的表!

Each reference is to different tables!

我在每个表上只能有一个删除级联吗?

I can have just one on delete cascade per table?

Edit: I think I found the problem, the bug is EF:

如果我有3个表:Users,Posts和Votes。

If I have 3 tables: Users, Posts and Votes.

及其关系:

Post.AuthorId-> User.Id
Votes.PostId-> Post.Id
票.UserId-> User.Id

Post.AuthorId -> User.Id Votes.PostId -> Post.Id Votes.UserId -> User.Id

Sql会正确生成一个错误,因为如果删除帖子,我将有2条投票路径:User-> Vote and User- >发布->投票

Sql correctly generate a error, because if I delete a post, I will have 2 path to Votes: User -> Vote and User -> Post -> Vote

但是实体框架没有检测到该问题,并使用删除级联生成了3个外键。

But entity framework dont detect this and generate the 3 foreign key with on delete cascade.

推荐答案


编辑:我认为我发现了问题,该错误是EF

I think I found the problem, the bug is EF

我认为您是正确的。 此ADO.NET团队博客文章似乎在注释中解决了该问题,即在PostConfiguration类中设置CascadeOnDelete = false。

I think you are correct. This ADO.NET team blog post seems to address the problem in comments i.e. setting CascadeOnDelete = false in the PostConfiguration class.

这篇关于引入FOREIGN KEY约束可能会导致循环或多个级联路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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