tlyng
tlyng

Reputation: 87

Why is IdentityUser_ID column of AspNetUserRoles table being set to NULL

I have a solution based on Entity Framework 6.1.0, ASP.NET Identity Core 2.0.0, ASP.NET Identity EntityFramework 2.0.0, ASP.NET Identity OWIN 2.0.0.

When upgrading to the newest releases of the packages (EF 6.1.3, Identity packages rel. 2.2.1) using NuGet, I get the problem that the IdentityUser_ID column of AspNetUserRoles table is being set to NULL, when making changes to the user account.

I have seen few people mention having the same problem, but nowhere with a link to a solution for it. For one thing, I don't understand why there should be an IdentityUser_Id column in the first place, as it initially contains the same value as the UserId column.

Did anyone experience something similar when upgrading these packages, and what was your solution to overcome the problem?

Upvotes: 1

Views: 1325

Answers (1)

tlyng
tlyng

Reputation: 87

Cannot really explain exactly why it worked, but I made these changes in my code to overcome the problem:

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

    }


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

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

... and my 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");
}

So, generally commented out everything and changed from IdentityUser to ApplicationUser. After these changes, db migrations removed the extra columns from the identity tables.

Upvotes: 1

Related Questions