ASP.NET Core Identity Tables Customization (Identity Table Renaming)



Published On Thursday June 4, 2020
Reading Time: < 1 minute

When we start a .net core application using Asp.Net Core Identity once we complete our first migration and update it will create some table with predefined name like AspNetUsers, AspNetRoles and some others, but when we architect a new project we want the table to be name with our own standards and naming conventions.

So here are some simple line of code that will make the naming possible:

Head over to Data/ApplicationDbContext.cs

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
 
//Add These Lines of Code To Modify Table Name
builder.Entity<ApplicationUser>().ToTable("TBL_USERS"); //AspNetUsers
builder.Entity<IdentityRole>().ToTable("TBL_ROLES"); //AspNetRoles
builder.Entity<IdentityUserRole<string>>().ToTable("TBL_USER_ROLE"); //AspNetUserRole
builder.Entity<IdentityUserClaim<string>>().ToTable("TBL_USER_CLAIM"); //AspNetUserClaim
builder.Entity<IdentityUserLogin<string>>().ToTable("TBL_USER_LOGIN"); //AspNetUserLogin
builder.Entity<IdentityRoleClaim<string>>().ToTable("TBL_ROLE_CLAIM"); //AspNetRoleClaim
builder.Entity<IdentityUserToken<string>>().ToTable("TBL_USER_TOKEN"); //AspNetUserToken
 
}

So, Once the code is added and saved we again need to add migration and update the database as

//First Add Migration
 
add-migration migrationName
 
//Second Update Database
 
update-database

So, once the process completes the table name is renamed and we are good to go 🙂

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x