gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 23:11:01 26 4
gpt4 key购买 nike

我正在尝试扩展 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);
}
}

}

最佳答案

我通过编辑迁移并为主键添加删除和添加命令来解决它。
在新迁移的顶部,添加:

migrationBuilder.DropPrimaryKey("PK_AspNetUserTokens", "AspNetUserTokens");
以及对 AspNetUserTokens 的所有修改已经发生,添加
migrationBuilder.AddPrimaryKey("PK_AspNetUserTokens", "AspNetUserTokens", new[] { "UserId", "LoginProvider", "Name" });

关于c# - 对象 'PK_AspNetUserTokens' 依赖于列 'Name' 。 ALTER TABLE ALTER COLUMN Name 失败,因为一个或多个对象访问此列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57236708/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com