对于您当前的模型设计,它将创建以下迁移:
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_ChildUserRoleId
and FK_UserRoleRelationship_UserRole_ParentUserRoleId
两者都会删除其中的记录UserRole
删除时UserRoleRelationship
这会导致多次级联删除。
对于解决方法,请尝试使int
as int?
像下面这样:
public int? ParentUserRoleId { get; set; }
这将创建
migrationBuilder.AddForeignKey(
name: "FK_UserRoleRelationship_UserRole_ParentUserRoleId",
table: "UserRoleRelationship",
column: "ParentUserRoleId",
principalTable: "UserRole",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
Note
你需要删除UserRole
首先,然后删除UserRoleRelationship