引入FOREIGN KEY约束可能会导致循环或多个级联路径?

编程入门 行业动态 更新时间:2024-10-25 18:24:01
本文介绍了引入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约束可能会导致循环或多个级联路径?

本文发布于:2023-10-18 11:31:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   可能会   路径   级联   FOREIGN

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!