Search code examples
c#entity-framework-migrationsasp.net-core-identityasp.net-core-2.1ef-core-2.1

How to Rename AspNetUsers table for a class inherited from IdentityUser in ASP .NET Core 2.1?


I'm using ASP .NET Core 2.1 for my project with IndividualAuthentication. I need extra properties for my User table so i inherited from IdentityUser like below :

 public class ApplicationUser : IdentityUser
{
    [Required]
    [DataType(DataType.Text)]
    public string Name { get; set; }

    [Required]
    [DataType(DataType.Text)]
    public string LastName { get; set; }
}

After this modification AspNetUsers table is not renamed. All other identity tables are renamed. I don't know why is this happening.

After creating ApplicationUser class i have replaced IdentityUser with ApplicationUser in my code of Startup.cs

Below is the code before Modification

services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<IdentityUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

After modification

services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));
        services.AddDefaultIdentity<ApplicationUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

This is my OnModelCreating method

protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        base.OnModelCreating(builder);

        modelBuilder.Entity<ApplicationUser>().ToTable("User");
        //modelBuilder.Entity<IdentityUser>().ToTable("User");
        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<IdentityUserClaim<string>().ToTable("UserClaim");
        modelBuilder.Entity<IdentityUserRole<string>>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserLogin<string>().ToTable("UserLogin");
        modelBuilder.Entity<IdentityRoleClaim<string>().ToTable("RoleClaim");
        modelBuilder.Entity<IdentityUserToken<string>().ToTable("UserToken");
    }

Now i don't know what other thing i am missing for renaming AspNetUsers table. I didn't find any solution and still searching.


Solution

  • The fluent configuration is ok, but the identity class used as base for your context is not.

    As explained in the Customizing the model (emphasis is mine):

    The starting point for customizing the model is to derive from the appropriate context type; see the preceding section.

    and the preceding sections explain the base classes, generic type arguments and the default configuration.

    With that being said, since you are using only a custom IdentityUser derived class, the base at least should be the IdentityDbContext<TUser>:

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        // ...
    }