添加迁移时,IdentityServer DB 上的 EF Core 迁移适用于多个 table 而不是仅 AspNetUsers table

EF Core Migration on IdentityServer DB applies for MULTIPLE tables instead of ONLY AspNetUsers table when adding the migration

我想在我的 AspNetUsers table 上添加一个新字段。我想通过 EF Migration 做到这一点。

在我的 InitialCreate 迁移中,我只添加了 FullName(table 中的最后一个字段)- 构建成功。

在我的第二次迁移中,我想添加另一个字段。我更新了派生自 IdentityUser

的 ApplicationUser
public class ApplicationUser : IdentityUser
{
    [Column(TypeName = "nvarchar(150)")]
    public string FullName { get; set; }

    public string RefreshTokensAuth { get; set; }
}

问题是我在PM中运行命令后:

PM> Add-Migration -Context AuthenticationContext AddUsersSession

我的迁移包含对其他 table 的修改以及如下所示(AspNetUserTokens、AspNetUserLogins),不仅针对 AspNetUsers:

public partial class AddUsersSession : Migration
{
    protected override void Up(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.AlterColumn<string>(
            name: "Name",
            table: "AspNetUserTokens",
            maxLength: 128,
            nullable: false,
            oldClrType: typeof(string),
            oldType: "nvarchar(450)");

        migrationBuilder.AlterColumn<string>(
            name: "LoginProvider",
            table: "AspNetUserTokens",
            maxLength: 128,
            nullable: false,
            oldClrType: typeof(string),
            oldType: "nvarchar(450)");

        migrationBuilder.AlterColumn<string>(
            name: "FullName",
            table: "AspNetUsers",
            type: "nvarchar(150)",
            nullable: true,
            oldClrType: typeof(string),
            oldType: "nvarchar(150",
            oldNullable: true);

        migrationBuilder.AddColumn<string>(
            name: "RefreshTokensAuth",
            table: "AspNetUsers",
            nullable: true);

        migrationBuilder.AlterColumn<string>(
            name: "ProviderKey",
            table: "AspNetUserLogins",
            maxLength: 128,
            nullable: false,
            oldClrType: typeof(string),
            oldType: "nvarchar(450)");

        migrationBuilder.AlterColumn<string>(
            name: "LoginProvider",
            table: "AspNetUserLogins",
            maxLength: 128,
            nullable: false,
            oldClrType: typeof(string),
            oldType: "nvarchar(450)");
    }

    protected override void Down(MigrationBuilder migrationBuilder)
    {
        migrationBuilder.DropColumn(
            name: "RefreshTokensAuth",
            table: "AspNetUsers");

        migrationBuilder.AlterColumn<string>(
            name: "Name",
            table: "AspNetUserTokens",
            type: "nvarchar(450)",
            nullable: false,
            oldClrType: typeof(string),
            oldMaxLength: 128);

        migrationBuilder.AlterColumn<string>(
            name: "LoginProvider",
            table: "AspNetUserTokens",
            type: "nvarchar(450)",
            nullable: false,
            oldClrType: typeof(string),
            oldMaxLength: 128);

        migrationBuilder.AlterColumn<string>(
            name: "FullName",
            table: "AspNetUsers",
            type: "nvarchar(150",
            nullable: true,
            oldClrType: typeof(string),
            oldType: "nvarchar(150)",
            oldNullable: true);

        migrationBuilder.AlterColumn<string>(
            name: "ProviderKey",
            table: "AspNetUserLogins",
            type: "nvarchar(450)",
            nullable: false,
            oldClrType: typeof(string),
            oldMaxLength: 128);

        migrationBuilder.AlterColumn<string>(
            name: "LoginProvider",
            table: "AspNetUserLogins",
            type: "nvarchar(450)",
            nullable: false,
            oldClrType: typeof(string),
            oldMaxLength: 128);
    }
}

当然,当我运行

update-database -context AuthenticationContext

我运行出错了。

有人经历过吗?任何提示都会有所帮助。谢谢!

更新:

我发现我的 InitialCreate 迁移有一些问题。

我在未对 IdentityUser 模型进行任何更改的情况下通过 运行ning Add-Migration 进行了复制,并且仍然获得了 Up 和 Down 自动生成的更改。

为了解决这个问题,我 运行 像以前一样添加迁移,通过删除将更新不应更新的表的字段(代码行)来更改迁移,然后 运行

Update-Database -Context AuthenticationContext

下一次迁移(如果您打算进行其他迁移)运行 就好了,因为我做了进一步的测试和检查。

谢谢!