1
votes

Similar Problem as in Why Identity 2.0 adds a new column in AspNetUserRoles when I extend IdentityUser? My ApplicationUser class:

public class ApplicationUser : IdentityUser
{
  public ApplicationUser()
  {
    Address = new Address ();
  }
  public string Firstname{ get; set; }
  public string Lastname{ get; set; }
  public Address Address{ get; set; }
  public bool IsActivated { get; set; }
}

And the context:

public class DatabaseContext : IdentityDbContext<ApplicationUser>
{
  static DatabaseContext()
  {
    Database.SetInitializer<DatabaseContext>(null);
  }

  public DatabaseContext()
    : base(nameOrConnectionString: "Archive")
  {
  }

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
  {
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<IdentityUser>().ToTable("Users").Property(p => p.Id).HasColumnName("Id");
    modelBuilder.Entity<ApplicationUser>().ToTable("Users").Property(p => p.Id).HasColumnName("Id");
}

The created columns in table AspNetUserRoles are: UserId, RoleId and IdentityUser_Id. If I assign a role to a user, only UserId and RoleId are filled. If I want to retrieve in which roles a user is, nothing comes back.

What I'm doing wrong?

1

1 Answers

1
votes

Actually, there should not be column called IdentityUser_Id in IdentityUserRole table. And I hope you are using entity ramework code first approach.

Try changing your OnModelCreating method as below. Currently you have created your ApplicationUser table by inheriting from IdentityUser. So that, you don't need to model bind for IdentityUser.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<ApplicationUser>().ToTable("Users");
}

Hope this helps.