为什么 AspNetUserRoles table 的 IdentityUser_ID 列被设置为 NULL

Why is IdentityUser_ID column of AspNetUserRoles table being set to NULL

我有一个基于 Entity Framework 6.1.0,ASP.NET Identity Core 2.0.0,ASP.NET Identity EntityFramework 2.0.0,ASP.NET 身份 OWIN 2.0.0。

使用 NuGet 升级到最新版本的包(EF 6.1.3,Identity packages rel. 2.2.1)时,我遇到的问题是 AspNetUserRolesIdentityUser_ID 列 table 在更改用户帐户时被设置为 NULL。

我看到很少有人提到有同样的问题,但没有 link 解决方案。一方面,我不明白为什么首先要有一个 IdentityUser_Id 列,因为它最初包含与 UserId 列相同的值。

有没有人在升级这些软件包时遇到过类似的问题,您是如何解决这个问题的?

无法准确解释其工作原理,但我在代码中进行了这些更改以解决该问题:

public class MyContext : IdentityDbContext<ApplicationUser>
{
    public MyContext() : base("MyConnection")
    {

    }


    static MyContext()
    {
        Database.SetInitializer<MyContext>(new ApplicationDbInitializer());
    }

    public static MyContext Create()
    {
        return new MyContext();
    }

...和我的 OnModelCreating ...

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
    base.OnModelCreating(modelBuilder);
    modelBuilder.Entity<ApplicationUser>().ToTable("AspNetUsers");

//modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id).ToTable("AspNetRoles");
//modelBuilder.Entity<IdentityUser>().ToTable("AspNetUsers");
//modelBuilder.Entity<IdentityUserLogin>().HasKey(l => new { l.UserId, l.LoginProvider, l.ProviderKey }).ToTable("AspNetUserLogins");
//modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId }).ToTable("AspNetUserRoles");
//modelBuilder.Entity<IdentityUserClaim>().ToTable("AspNetUserClaims");
}

因此,通常注释掉所有内容并将 IdentityUser 更改为 ApplicationUser。在这些更改之后,数据库迁移从标识表中删除了额外的列。