Customizing Identity Table Names in .Net Core 3.1

Posted On March 13, 2020

Customizing the default table table names in Identity can seem like an overwhelming task. Luckily, it's very simple. We can follow a few easy steps to have our table names be readable and easier to use in our application.

Having your tables named "AspNetUsers" or similar can be very frustrating to look at. Being able to customize these names will help our code look nicer, stay organized, and follow general naming conventions of our tables.

DB Context

The first step is to add some code to our applications DB Context. In this case our class is called ApplicationDbContext.

The first thing we'll do is add an override to our OnModelCreating method.

protected override void OnModelCreating(ModelBuilder modelBuilder)

    // Override default AspNet Identity table names
        => { entity.ToTable(name: "Users"); });
        => { entity.ToTable(name: "Roles"); });
        => { entity.ToTable("UserRoles"); });
        => { entity.ToTable("UserClaims"); });
        => { entity.ToTable("UserLogins"); });
        => { entity.ToTable("UserTokens"); });
        => { entity.ToTable("RoleClaims"); });

This will chagne our table names to the string within the ToTable.

For example, this line turns the AspNetUsers table into Users:

modelBuilder.Entity<User>(entity => { entity.ToTable(name: "Users"); });

We'll also need to inherit from IdentityDbContext. For this we'll have to add the inheritance to our ApplicationDbContext class

public class ApplicationDbContext : IdentityDbContext<User>
User Model

In the above section we reference a User class in a few spots. This is our user model that can be used to customize columns in our user table. For this we'll need to create a new model.

In your Models folder add a new class called User.cs. In this class we'll be inheriting from the IdentityUser.

public class User : IdentityUser
    //Additional properties for your table would go here

The last piece of this change is that anywhere you would have used IdentityUser you will have to change that to User. An example of where this change needs to take place is in Views/Shared/_LoginPartial.cshtml. The current @inject would need the model being called to be changed to User.

@inject SignInManager<User> SignInManager
@inject UserManager<User> UserManager

If you are using the scaffolded items from identity you'll also want to check within those for lines such as

private readonly UserManager<User> _userManager;
private readonly SignInManager<User> _signInManager;

Dan Rovito

Software Developer

Hello there! I am a software developer based in Cincinnati, OH. I'd love to connect with you. Reach out on Twitter.