对象 'PK_AspNetUserTokens' 依赖于列 'Name'。 ALTER TABLE ALTER COLUMN Name 失败,因为一个或多个对象访问此列

The object 'PK_AspNetUserTokens' is dependent on column 'Name'. ALTER TABLE ALTER COLUMN Name failed because one or more objects access this column

我正在尝试扩展 IdentityUser class。我添加了一个新的 class ApplicationUser 并继承了 IdentityUser class。迁移已成功添加,但在更新数据库时,出现错误 "The object 'PK_AspNetUserTokens' is dependent on column 'Name'.ALTER TABLE ALTER COLUMN Name failed because one or more objects access this column."。我打开 SSMS 并在 AspNetUserToken 中查找数据,table 是空的。

我已经尝试了几件事,但最终都出现了同样的错误。我在代码中替换了对 IdentityUser class 的所有引用。删除table'AspNetUsers'中的数据。替换引用和删除数据后删除迁移。再次添加migration和update database,错误依旧。

AppDbContext.cs


namespace PieShop.Data_Access_Layer
{
    public class AppDbContext :IdentityDbContext<ApplicationUser>
    {
        public AppDbContext(DbContextOptions<AppDbContext> options)
            :base(options)
        {
        }
        public DbSet<Pie> Pies { get; set; }
        public DbSet<Feedback> Feedbacks { get; set; }
    }
}


IdentityHostingStartup.cs


[assembly: HostingStartup(typeof(PieShop.Areas.Identity.IdentityHostingStartup))]
namespace PieShop.Areas.Identity
{
    public class IdentityHostingStartup : IHostingStartup
    {
        public void Configure(IWebHostBuilder builder)
        {
            builder.ConfigureServices((context, services) => {
                services.AddDefaultIdentity<ApplicationUser>().AddEntityFrameworkStores<AppDbContext>();
            });
        }
    }
}
ApplicationUser.cs


namespace PieShop.Models
{
    public class ApplicationUser : IdentityUser
    {
        [Required]
        [MaxLength(30)]
        public string City { get; set; }
        [Required]
        public string Address  { get; set; }
        [Required]
        [MaxLength(20)]
        public string Country { get; set; }
    }
}

迁移

using Microsoft.EntityFrameworkCore.Migrations;

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

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

        migrationBuilder.AddColumn<string>(
            name: "Address",
            table: "AspNetUsers",
            nullable: false,
            defaultValue: "");

        migrationBuilder.AddColumn<string>(
            name: "City",
            table: "AspNetUsers",
            maxLength: 30,
            nullable: false,
            defaultValue: "");

        migrationBuilder.AddColumn<string>(
            name: "Country",
            table: "AspNetUsers",
            maxLength: 20,
            nullable: false,
            defaultValue: "");

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

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

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

        migrationBuilder.DropColumn(
            name: "City",
            table: "AspNetUsers");

        migrationBuilder.DropColumn(
            name: "Country",
            table: "AspNetUsers");

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

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

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

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

}

我通过编辑迁移并为主键添加删除和添加命令解决了这个问题。

在新迁移的顶部,添加:

migrationBuilder.DropPrimaryKey("PK_AspNetUserTokens", "AspNetUserTokens");

在对 AspNetUserTokens 进行所有修改后,添加

migrationBuilder.AddPrimaryKey("PK_AspNetUserTokens", "AspNetUserTokens", new[] { "UserId", "LoginProvider", "Name" });