Felipe Pessoto
Felipe Pessoto

Reputation: 6969

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.

Upvotes: 1

Views: 3802

Answers (1)

onedaywhen
onedaywhen

Reputation: 57023

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.

Upvotes: 3

Related Questions