Search code examples
c#entity-framework-6asp.net-web-api2asp.net-identity-2

ASP.net Identity custom user model not being used by roles, claims and login table


I am using a custom user model like so:

 public class ApplicationUser : IdentityUser
 {
    public string Firstname { get; set; }
    ...
 }

The rest of the tables created by the Identity system are a bit out of sync. A new column is created called ApplicationUser_Id which is set at the foreign key but there is still a UserId which is used a composite primary key.

Generated tables

Configuration for ApplicationUser model is

public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
{
    public ApplicationUserConfiguration()
    {
        this.ToTable("AspNetUsers");
        this.Property(u => u.Firstname).IsRequired().HasMaxLength(100);
        this.Property(u => u.Lastname).IsRequired().HasMaxLength(100);
    }
}

How do I tell EF to continue using UserId as the foreign key and get rid of ApplicationUser_Id?

Thanks

UPDATE: Working as expected until I add custom configuration for ApplicationUser.

DataContext.cs

modelBuilder.Configurations.Add(new ApplicationUserConfiguration());

ApplicationUserConfiguration.cs

public class ApplicationUserConfiguration : EntityTypeConfiguration<ApplicationUser>
{
    public ApplicationUserConfiguration()
    {
        this.ToTable("AspNetUsers");
        this.Property(u => u.Firstname).IsRequired().HasMaxLength(100);
        this.Property(u => u.Lastname).IsRequired().HasMaxLength(100);
    }
}

Solution

  • I did reproduce the issue then i fixed it by adding modelBuilder.Configurations.Add(new ApplicationUserConfiguration()); before base.OnModelCreating(modelBuilder); so the function now

      protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Configurations.Add(new ApplicationUserConfiguration());
            base.OnModelCreating(modelBuilder);
    
        }