[英]Entity Framework, Foreign key constraint may cause cycles or multiple cascade paths
I use Entity Code first for my project. 我首先为项目使用实体代码。 Basically I have 3 class Users
, Branchs
and UsersBranchs
. 基本上我有3类Users
, Branchs
和UsersBranchs
。
Users
contains UserID
, Name
,... Users
包含UserID
, Name
,...
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 Branchs
包含BranchID
, Location
,...和UserID,这是指分支的创建者,而UsersBranchs
仅具有两列BranchID和UserID,用于定义哪个用户位于哪个分支中
the problem is I get this error: 问题是我收到此错误:
'FK_dbo.UsersBranchs_dbo.Users_UsersID' on table 'UsersBranchs' may cause cycles or multiple cascade paths. 表“ UsersBranchs”上的“ FK_dbo.UsersBranchs_dbo.Users_UsersID”可能会导致循环或多个级联路径。 Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. 指定ON DELETE NO ACTION或ON UPDATE NO ACTION,或修改其他FOREIGN KEY约束。
Can you help me please? 你能帮我吗?
Update 更新
It's UsersBranchs Class 这是UsersBranchs类
[ForeignKey("UserID")]
public CoreUsers User { get; set; }
public Guid UsersID { get; set; }
[ForeignKey("BranchID")]
public Branchs Branch { get; set; }
public Guid BranchID { get; set; }
And also add this line to DbContext class to use both UserID and BranchID as key 并将此行添加到DbContext类以使用UserID和BranchID作为键
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 .....
Users Class is 现“用户类别”为
[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. 长期以来,Sql Server一直无法处理多个级联路径并无法将其级联删除到同一表 。 Just Google the error message. 仅Google错误消息。 Basically, if you want to use cascade delete then you'll have to make sure that there is only a single cascade path. 基本上,如果要使用级联删除,则必须确保只有一个级联路径。
At the moment you have two paths from Branchs -> UsersBranchs and Branchs -> Users -> UsersBranchs. 目前,您有两个来自Branchs-> UsersBranchs和Branchs-> Users-> UsersBranchs的路径。
EF by default sets cascade delete but it may be stopped by removing the conventions in your DbContext. EF默认情况下会设置级联删除,但是可以通过删除DbContext中的约定来停止它。
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Manually set cascade delete behaviour
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
modelBuilder.Conventions.Remove<ManyToManyCascadeDeleteConvention>();
base.OnModelCreating(modelBuilder);
}
Then you'll have to set WillCascadeOnDelete(true) on any relationships where you want cascade delete. 然后,您必须在要级联删除的任何关系上设置WillCascadeOnDelete(true)。 See the Entity Framework documentation . 请参阅实体框架文档 。
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. 您看起来好像在尝试创建多对多链接/联接表UsersBranchs,但是在Branchs上也只有一个User并没有什么意义。 Do you even need the UsersBranchs table in that case? 在这种情况下,您甚至需要UsersBranchs表吗? Did you mean to have a collection of Users on your Branchs, ie 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. 顺便说一句,我真的不喜欢对单个实体使用复数。
I think you are getting the problem because you didn't tell Entity framework how it will treat these classes on delete on cascade 我认为您遇到了问题,因为您没有告诉Entity Framework在层叠删除时如何处理这些类
in your DbContext class, override the OnModelCreating method and write this code 在您的DbContext类中,重写OnModelCreating方法并编写此代码
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<UserBranch>()
.HasRequired(t => t.CoreUsers)
.WithMany()
.HasForeignKey(t => t.UserID)
.WillCascadeOnDelete(false);
modelBuilder.Entity<UserBranch>()
.HasRequired(t => t.Branch)
.WithMany()
.HasForeignKey(t => t.BranchID)
.WillCascadeOnDelete(false);
modelBuilder.Entity<Branch>()
.HasRequired(t => t.User)
.WithMany()
.HasForeignKey(t => t.UserID)
.WillCascadeOnDelete(false);
}
hope this will help you 希望这个能对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.