【发布时间】: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