且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

实体框架:如何解决“FOREIGN KEY约束可能导致周期或多个级联路径”?

更新时间:2023-02-03 08:16:17

使用流畅的api指定错误消息建议的操作。

You can use the fluent api to specify the actions the error message suggests.

在您的上下文中:

protected override void OnModelCreating( DbModelBuilder modelBuilder )
{
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<aspnet_UsersInRoles>().HasMany(i => i.Users).WithRequired().WillCascadeOnDelete(false);
}

请注意,您没有包含表aspnet_UsersInRoles的定义,因此这段代码可能不起作用。

Note that you have not included the definition for the table aspnet_UsersInRoles so this code may not work.

另一个选项是通过添加此

Another option is to remove all CASCADE DELETES by adding this

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

如果您需要更多有关配置流畅api关系的信息,我建议 http://msdn.microsoft.com/en-US/data/jj591620

If you need more info about configuring relationships with the fluent api I suggest http://msdn.microsoft.com/en-US/data/jj591620