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

问题描述 投票:0回答:2

我正在尝试扩展 IdentityUser 类。我添加了一个新类ApplicationUser并继承IdentityUser类。迁移已成功添加,但在更新数据库时,我收到错误“对象‘PK_AspNetUserTokens’依赖于列‘Name’。ALTER TABLE ALTER COLUMN Name 失败,因为一个或多个对象访问此列。”。我打开SSMS并在AspNetUserToken中查找数据,表是空的。

我尝试过一些事情,但最终出现了同样的错误。我替换了代码中对 IdentityUser 类的所有引用。删除表“AspNetUsers”中的数据。替换引用并删除数据后删除迁移。再次添加迁移并更新数据库,错误依然存在。

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);
    }
}

}

c# asp.net-mvc entity-framework asp.net-core entity-framework-core-2.2
2个回答
9
投票

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

在新迁移的顶部添加:

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

在对

AspNetUserTokens
进行所有修改后,添加

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

0
投票

我遇到了类似的问题并解决了这个问题

1-删除数据库和迁移: 首先,我删除了数据库和所有现有的迁移。这确保了工作一切顺利。

2-重新创建数据库和迁移: 然后,我从头开始重建数据库和迁移

© www.soinside.com 2019 - 2024. All rights reserved.