Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
952 views
in Technique[技术] by (71.8m points)

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

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...