实体框架,外键约束可能会导致循环或多个级联路径 [英] Entity Framework, Foreign key constraint may cause cycles or multiple cascade paths

查看:60
本文介绍了实体框架,外键约束可能会导致循环或多个级联路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我首先为项目使用实体代码.基本上我有3个类 Users Branchs UsersBranchs .

I use Entity Code first for my project. Basically I have 3 class Users,Branchs and UsersBranchs.

Users 包含 UserID Name ,...

分支包含 BranchID Location ,...和与分支创建者相关的UserID和 UsersBranchs 仅具有两列BranchID和UserID,用于定义哪个用户位于哪个分支中

Branchs contains BranchID, Location, ... and UserID which is refer to creator of branch and UsersBranchs just have have two column BranchID and UserID which is define which user is in which branch

问题是我收到此错误:

表"UsersBranchs"上的"FK_dbo.UsersBranchs_dbo.Users_UsersID"可能导致循环或多个级联路径.指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束.

'FK_dbo.UsersBranchs_dbo.Users_UsersID' on table 'UsersBranchs' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.

你能帮我吗?

更新
这是UsersBranchs类

Update
It's UsersBranchs Class

[ForeignKey("UserID")]
public CoreUsers User { get; set; }
public Guid UsersID { get; set; }

[ForeignKey("BranchID")]
public Branchs Branch { get; set; }
public Guid BranchID { get; set; }


并将这行添加到DbContext类中,以同时使用UserID和BranchID作为


And also add this line to DbContext class to use both UserID and BranchID as key


modelBuilder.Entity<UsersBranchs>().HasKey(x => new { x.UserID, x.BranchID });


分支机构类别为


Branchs Class is

   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   [Key]
   public Guid ID { get; set; }

   [ForeignKey("UserID")]
   public CoreUsers User { get; set; }
   public Guid UserID { get; set; }

   public .....


用户类别为

   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   [Key]
   public Guid ID { get; set; }

   public .....

推荐答案

无法

Not being able to handle multiple cascade paths and cascade delete to same table has been a limitation of Sql Server for a long time. Just Google the error message. Basically, if you want to use cascade delete then you'll have to make sure that there is only a single cascade path.

目前,您有两条来自分支"->"UsersBranchs"和分支->"Users"->"UsersBranchs"的路径.

At the moment you have two paths from Branchs -> UsersBranchs and Branchs -> Users -> UsersBranchs.

默认情况下,EF会设置级联删除,但是可以通过删除DbContext中的约定来停止它.

EF by default sets cascade delete but it may be stopped by removing the conventions in your DbContext.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    // Manually set cascade delete behaviour
    modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();

    base.OnModelCreating(modelBuilder);
}

然后,您必须在要级联删除的任何关系上设置WillCascadeOnDelete(true).请参阅实体框架文档.

Then you'll have to set WillCascadeOnDelete(true) on any relationships where you want cascade delete. See the Entity Framework documentation.

除此之外,您的模型似乎有些奇怪.您看起来好像试图建立一个多对多链接/联接表UsersBranchs,但是您在Branchs上也只有一个User并没有任何意义.在这种情况下,您甚至需要UsersBranchs表吗?您是不是要在您的分支机构上拥有一组用户,即导航属性而不是外键,从而提供了一对多的关系分支机构->用户?

Other than that your model seems a bit strange. You look like you're trying to make a many-to-many link/join table, UsersBranchs, but you also have a single User on the Branchs which doesn't really make sense. Do you even need the UsersBranchs table in that case? Did you mean to have a collection of Users on your Branchs, i.e. a navigation property rather than a foreign key, which gives a one-to-many relationship Branchs -> Users?

顺便说一句,我真的不喜欢对单个实体使用复数.

As an aside I really dislike using plurals for single entities.

这篇关于实体框架,外键约束可能会导致循环或多个级联路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆