September 05, 2017

Taking Control of ASP.NET Identity Table Names

If you are like me, I love the power, and ease of use that comes with ASP.NET Identity. However, I find that the naming conventions for model objects related to identity do not necessarily meet the standards that I would like to use in my applications. As such, I find it necessary to take control of these names and work to standardize my projects, and this post explores this process.

The Goal

Before I get into any specifics, let's discuss our target; in the end, I would like a database where all tables are named consistently and any of my naming desires implemented through the entire application. For myself, this results in a goal of avoiding table names such as aspnetUsers and ensuring no columns have a name of only id. In the end, your specific rules can be addressed and could be different than mine.

My goal at this point is to remove the "Aspnet" prefix from all of the primary tables, with a secondary consideration to rename a few of the "id" columns to more descriptive items such as UserId, RoleId, and UserRoleId.

Making the Change

The best way to make the change is to add code into the "OnModelCreating" method inside of your DB context. However, it is very important to place this code at the proper location. You must insert any customizations AFTER the call to base.OnModelCreating(builder), if you do not do this, your changes will appear to have no impact. In my case, I was able to accomplish my goal using the following snippet as my OnModelCreating method body.


builder.Entity<UserProfile>().Property(up => up.Id).HasColumnName("UserId");
builder.Entity<UserRole>().ToTable("Role").Property(ur => ur.Id).HasColumnName("RoleId");
builder.Entity<UserRole>().Property(ur => ur.Id).HasColumnName("UserRoleId");
builder.Entity<IdentityUserRole<int>>().ToTable("UserRole").HasKey(k => new {k.RoleId, k.UserId});

Working through this, we can see with the first two lines I work to adjust the UserProfile object to point it to a table name of UserProfile and rename the property of Id to be UserId. I started with these columns as many people have tried to use the [Column] attribute to rename, sadly as much as that works with other tables, it doesn't work here!

The rest of modifications I have included here are simply for the other standard identity tables. You don't need to adjust these unless you want to, but the end result makes for a clean structure.


Just because we use Identity, doesn't mean that we are stuck with their naming conventions all the time. How far you take this, is up to you, but it is easy to make your project consistent across the board.

Did this help? Or am I missing a key table? Be sure to share in the comments below!

tags: ASP.NET MVC, Entity Framework, .NET Core
comments powered by Disqus

Content provided in this blog is provided "AS-IS" and the information should be used at your own discretion.  The thoughts and opinions expressed are the personal thoughts of Mitchel Sellers and do not reflect the opinions of his employer.

Content Copyright

Content in this blog is copyright protected.  Re-publishing on other websites is allowed as long as proper credit and backlink to the article is provided.  Any other re-publishing or distribution of this content is prohibited without written permission from Mitchel Sellers.