【问题标题】:... may cause cycles or multiple cascade paths. 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 约束
【发布时间】:2018-09-11 05:04:40
【问题描述】:

实体框架核心

更新数据库时抛出错误

错误:- 在表“UserRoleRelationship”上引入 FOREIGN KEY 约束“FK_UserRoleRelationship_UserRoels_ParentUserRoleId”可能会导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束。 无法创建约束或索引。

public class UserRoleRelationship 
{
    [Key]
    public int Id { get; set; }
    [Required]
    public Guid UserRoleRelationshipId { get; set; }

    public virtual UserRole ChildUserRole { get; set; }
    public int ChildUserRoleId { get; set; }

    public virtual UserRole ParentUserRole { get; set; }
    public int ParentUserRoleId { get; set; }

}

public class UserRole 
{
    [Key]
    public int Id { get; set; }
    [Required]
    public Guid UserRoleId { get; set; }
    public virtual Role Role { set; get; }
    public int RoleId { set; get; }
    public virtual U.User User { set; get; }
    public int UserId { set; get; }
}

【问题讨论】:

  • 问题是什么?
  • 执行更新数据库时抛出错误错误:可能导致循环或多个级联路径。指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其他 FOREIGN KEY 约束
  • 我有10多个模型,哪个模型有FK显示这个错误。

标签: entity-framework asp.net-core migration entity-framework-core ef-core-2.0


【解决方案1】:

对于您当前的模型设计,它将在下面创建迁移:

            migrationBuilder.AddForeignKey(
            name: "FK_UserRoleRelationship_UserRole_ChildUserRoleId",
            table: "UserRoleRelationship",
            column: "ChildUserRoleId",
            principalTable: "UserRole",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);

        migrationBuilder.AddForeignKey(
            name: "FK_UserRoleRelationship_UserRole_ParentUserRoleId",
            table: "UserRoleRelationship",
            column: "ParentUserRoleId",
            principalTable: "UserRole",
            principalColumn: "Id",
            onDelete: ReferentialAction.Cascade);

FK_UserRoleRelationship_UserRole_ChildUserRoleIdFK_UserRoleRelationship_UserRole_ParentUserRoleId在删除UserRoleRelationship时都会删除UserRole中的记录,会导致多次级联删除。

要解决方法,请尝试将int 设置为int?,如下所示:

        public int? ParentUserRoleId { get; set; }

这将创建

migrationBuilder.AddForeignKey(
                name: "FK_UserRoleRelationship_UserRole_ParentUserRoleId",
                table: "UserRoleRelationship",
                column: "ParentUserRoleId",
                principalTable: "UserRole",
                principalColumn: "Id",
                onDelete: ReferentialAction.Restrict);

注意
需要先删除UserRole,再删除UserRoleRelationship

【讨论】:

  • 我相信你需要先删除UserRoleRelationship,然后再删除UserRole。如果存在引用它的 UserRoleRelationshipRestrict 将阻止 UserRole 删除。
【解决方案2】:

对于解决方法,请尝试将int 设置为int?,如下所示:

public int? ParentUserRoleId { get; set; }

【讨论】:

  • 是的,这就行了。此外,在 virtuals(例如 virtual Role Role)上声明 [Key, ForeignKey("... foreign key field name ...")],即在示例中:[Key, ForeignKey("RoleId")] virtual Role Role { get; set; }。这样,就会使用正确的导航属性创建迁移。
  • 完美哑光,坚持下去
  • 顺便说一句 - 你的回答拯救了我的一天。谢谢!
猜你喜欢
  • 1970-01-01
  • 2021-05-29
  • 2021-05-08
  • 1970-01-01
  • 2023-03-27
  • 2011-05-08
  • 2013-10-22
相关资源
最近更新 更多