实体框架,外键约束可能会导致循环或多个级联路径

编程入门 行业动态 更新时间:2024-10-26 06:31:01
本文介绍了实体框架,外键约束可能会导致循环或多个级联路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我首先为项目使用实体代码.基本上我有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 .....

推荐答案

无法处理多个级联路径和级联删除到同一表一直是Sql Server的限制.只是谷歌的错误信息.基本上,如果要使用级联删除,则必须确保只有一个级联路径.

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.

更多推荐

实体框架,外键约束可能会导致循环或多个级联路径

本文发布于:2023-10-18 11:33:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   可能会   实体   路径   框架

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!