Introducing FOREIGN KEY constraint may cause cycles or multiple cascade paths?

不羁的心 提交于 2019-11-28 06:26:35

问题


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:

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

And the relations:

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

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

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


回答1:


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

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.



来源:https://stackoverflow.com/questions/4150605/introducing-foreign-key-constraint-may-cause-cycles-or-multiple-cascade-paths

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!