【问题标题】:Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths?引入 FOREIGN KEY 约束可能会导致循环或多个级联路径?
【发布时间】:2011-05-08 05:28:13
【问题描述】:

当我从实体框架代码生成数据库时遇到这个错误。

我没有发现脚本有任何问题:

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;

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

每个表只能有一个删除级联?

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

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

还有关系:

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

Sql 正确生成错误,因为如果我删除帖子,我将有 2 个投票路径:用户 -> 投票和用户 -> 帖子 -> 投票

但实体框架没有检测到这一点并在删除级联时生成 3 个外键。

【问题讨论】:

    标签: sql entity-framework code-first


    【解决方案1】:

    编辑:我想我找到了问题,错误是 EF

    我认为你是对的。 This ADO.NET team blog post 似乎解决了 cmets 中的问题,即在 PostConfiguration 类中设置 CascadeOnDelete = false。

    【讨论】:

      猜你喜欢
      • 2013-10-22
      • 2018-08-08
      • 2017-04-26
      • 2019-10-20
      • 2018-12-14
      • 2013-06-12
      相关资源
      最近更新 更多