我正在尝试扩展IdentityUser类。我添加了一个新类ApplicationUser并继承了IdentityUser类。迁移成功添加,但是在更新数据库时,出现错误“对象'PK_AspNetUserTokens'依赖于列'名称'。ALTERTABLE ALTER COLUMN名称失败,因为一个或多个对象访问此列。”。我打开了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);
}
}
}
答案 0 :(得分:4)
我通过编辑迁移并为主键添加了drop和add命令来解决了这个问题。
在新迁移的顶部,添加:
migrationBuilder.DropPrimaryKey("PK_AspNetUserTokens", "AspNetUserTokens");
,并且在对AspNetUserTokens
进行了所有修改之后,添加
migrationBuilder.AddPrimaryKey("PK_AspNetUserTokens", "AspNetUserTokens", new[] { "UserId", "LoginProvider", "Name" });
答案 1 :(得分:1)
我认为您要添加一个列,该列已经存在于IdentyUser类中,其他类也依赖该列,并且它们的类型可能与现有属性不匹配。能否通过添加ApplicationUser类来更新您的问题。