C# 如何在 EF 代码优先中禁用链接表的级联删除?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13705441/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to disable cascade delete for link tables in EF code-first?
提问by Jez
I want to disable cascade deletes for a link table with entity framework code-first. For example, if many users have many roles, and I try to delete a role, I want that delete to be blocked unlessthere are no users currently associated with that role. I already remove the cascade delete convention in my OnModelCreating:
我想使用实体框架代码优先禁用链接表的级联删除。例如,如果许多用户有许多角色,而我尝试删除一个角色,我希望该删除被阻止,除非当前没有与该角色关联的用户。我已经删除了我的级联删除约定OnModelCreating:
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
...
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
And then I set up the user-role link table:
然后我设置了用户角色链接表:
modelBuilder.Entity<User>()
.HasMany(usr => usr.Roles)
.WithMany(role => role.Users)
.Map(m => {
m.ToTable("UsersRoles");
m.MapLeftKey("UserId");
m.MapRightKey("RoleId");
});
Yet when EF creates the database, it creates a delete cascade for the foreign key relationships, eg.
然而,当 EF 创建数据库时,它会为外键关系创建一个删除级联,例如。
ALTER TABLE [dbo].[UsersRoles] WITH CHECK ADD CONSTRAINT [FK_dbo.UsersRoles_dbo.User_UserId] FOREIGN KEY([UserId])
REFERENCES [dbo].[User] ([UserId])
ON DELETE CASCADE
GO
ALTER TABLE [dbo].[UsersRoles] WITH CHECK ADD CONSTRAINT [FK_dbo.UsersRoles_dbo.Role_RoleId] FOREIGN KEY([RoleId])
REFERENCES [dbo].[Role] ([RoleId])
ON DELETE CASCADE
GO
How can I stop EF generating this delete cascade?
如何停止 EF 生成此删除级联?
采纳答案by Jez
I got the answer. :-) Those cascade deletes were being created because of ManyToManyCascadeDeleteConvention. You need to remove this convention to prevent it from creating cascade deletes for link tables:
我得到了答案。:-) 这些级联删除是因为ManyToManyCascadeDeleteConvention. 您需要删除此约定以防止它为链接表创建级联删除:
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
回答by ebram khalil
I believe that turning off ManyToManyCascadeDeleteConventionglobally is not a wise option. Instead, it's better to turn it off only for the concernedtable.
我相信在ManyToManyCascadeDeleteConvention全球范围内关闭并不是一个明智的选择。相反,最好只为相关表关闭它。
This can be achieved through editing the generated migration file, for property cascadeDelete. For example:
这可以通过为 property 编辑生成的迁移文件来实现cascadeDelete。例如:
AddForeignKey("dbo.UsersRoles", "UserId", "dbo.User", "UserId", cascadeDelete: false);
AddForeignKey("dbo.UsersRoles", "UserId", "dbo.User", "UserId", cascadeDelete: false);
回答by TwainJ
I agree with Ebram Khalil that turning it off for a single table is a good option. I like to stick as close to the automatically built migrations as I can, however, so I would set it up in OnModelCreating:
我同意 Ebram Khalil 的观点,即为单张桌子关闭它是一个不错的选择。但是,我喜欢尽可能接近自动构建的迁移,因此我会在 OnModelCreating 中进行设置:
modelBuilder.Entity<User>()
.HasMany(usr => usr.Roles)
.WithMany(role => role.Users)
.Map(m => {
m.ToTable("UsersRoles");
m.MapLeftKey("UserId");
m.MapRightKey("RoleId");
})
.WillCascadeOnDelete(false);
I believe this preserves the delete going the other direction, so if both needed to be blocked (makes sense in this example) a similar call would need to be made starting with Entity<User>(Role)
我相信这会保留删除的另一个方向,所以如果两者都需要被阻止(在这个例子中是有意义的),则需要从 Entity<User>(Role)
Of course, this comes ages after the question was asked. So it may not have been valid in 2012.
当然,这是在提出问题后很久才出现的。所以它可能在 2012 年无效。

