我正在尝试扩展 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 …Run Code Online (Sandbox Code Playgroud) c# asp.net-mvc entity-framework asp.net-core entity-framework-core-2.2