我在aspnetusers表中又添加了2列,并添加了迁移。但是它失败并显示错误,表明FK_Projects_AspNetUsers_UserId'不是约束。 无法删除约束。查看以前的错误。
public string NavbarBackGroundImagePath { get; set; }
public string NavbarBackGroundColorCode { get; set; }
添加了这两列。
migrationBuilder.DropForeignKey(
name: "FK_Projects_AspNetUsers_UserId",
table: "Projects");
migrationBuilder.DropIndex(
name: "IX_Projects_UserId",
table: "Projects");
migrationBuilder.DropColumn(
name: "UserId",
table: "Projects");
migrationBuilder.AddColumn<int>(
name: "WorkspaceId",
table: "Projects",
nullable: false,
defaultValue: 0);
migrationBuilder.AddColumn<string>(
name: "NavbarBackGroundColorCode",
table: "AspNetUsers",
maxLength: 10,
nullable: true);
migrationBuilder.AddColumn<string>(
name: "NavbarBackGroundImagePath",
table: "AspNetUsers",
maxLength: 200,
nullable: true);
migrationBuilder.CreateIndex(
name: "IX_Projects_WorkspaceId",
table: "Projects",
column: "WorkspaceId");
migrationBuilder.AddForeignKey(
name: "FK_Projects_Workspaces_WorkspaceId",
table: "Projects",
column: "WorkspaceId",
principalTable: "Workspaces",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
但是在迁移文件中,它占用了所有这些东西。
答案 0 :(得分:0)
根据错误消息,如果Projects表不包含“ FK_Projects_AspNetUsers_UserId”约束或不包含“ UserId”,则当您要在Projects表中删除外键约束时,似乎发生此错误”属性,它将显示此错误。
因此,请检查数据库中的Projects表,是否已删除UerId列。以及Projects表是否具有AspNetUsers表的外键约束。如果Projects表不包含此约束,请尝试删除与DropForeignKey相关的代码:
migrationBuilder.DropForeignKey(
name: "FK_Projects_AspNetUsers_UserId",
table: "Projects");