ASP.NET Core Identity Tables Customization With Visual Studio 2017

In this article, we will use Visual Studio 2017 and Entity Framework Core step by step to learn how we can:

  1. Change Identity table names.
  2. Change Identity table property names.
  3. Change Identity table datatype of primary key.

When you decide to use ASP.NET Core Identity it will generate some tables used to store data for users and other things.

Prior to it, let’s take a brief definition about ASP.NET Core Identity.

Released on 2016 and from the name we can say it’s all about identity of the users, you can manage login and Logout functionality, create an account also you can use external login providers such as Facebook, Google, Microsoft Account and Twitter. In other words, you can say, it gives you the ability to make an authentication and an authorization.

Let us see it in action

Open your Visual Studio 2017

ASP.NET Core

I am using an Enterprise edition but you can use any edition, which is available to you. 

File > New > Project

Click ASP.NET Core Web Application and give it a name, as shown below.


ASP.NET Core

Select Web Application.

ASP.NET Core

Click Change authentication, select an individual user account and click OK, as shown below.

ASP.NET Core

Now, we have an Application but we need to make migration and we have several ways to achieve it. We will use Package Manager, as shown below.

ASP.NET Core

Now, you need to create the database. Go to appsettings.json file and edit your connection string, if you want. I am using SQL Server 2016, so this is my appsettings.json file and it looks, as shown below. 

  1. {  
  2.   "ConnectionStrings": {  
  3.     "DefaultConnection""Server=localhost;Database=aspnet-IdentityConfig;user id =sa;password=123456;MultipleActiveResultSets=true"  
  4.   },  
  5.   "Logging": {  
  6.     "IncludeScopes"false,  
  7.     "LogLevel": {  
  8.       "Default""Warning"  
  9.     }  
  10.   }  
  11. }   

Now, in Package Manager console, write Add-migration command, followed by the name of this migration command like this and wait for some seconds.

ASP.NET Core

Now, update the database and click enter.

ASP.NET Core

Now, go to the database and refresh it. You will find aspnet-IdentityConfig database with some tables created by default like AspNetUsers table.

We will use this table to achieve our goal.

ASP.NET Core

Now, how can we change this table name from AspNetUsers to AspUsers as example.

It's database stuff, so we can achieve this from ApplicationDbContext class.

ASP.NET Core

To be specific from OnModelCreating method into this class, we can customize our ASP.NET Identity model and override the defaults like change table name, property name and others.

We will change the table name now from AspNetUsers to AspUsers by this code inside OnModelCreating method. Proceed, as shown below.  

  1. protected override void OnModelCreating(ModelBuilder builder)  
  2. {  
  3.     base.OnModelCreating(builder);  
  4.   
  5.     builder.Entity<ApplicationUser>().ToTable("AspUsers");  
  6. }   

Note- After any customization in the tables, you should make database migration and update it to reflect the changes.

First, write add-migration. Subsequent to the migration, update the database.

ASP.NET Core

Now, refresh the database and you will find the name changed to AspUsers.

ASP.NET Core

Change Identity table property name

We need to change as an example Email to EmailAddress in AspUser table

ASP.NET Core

It’s very similar to what we did before, just we will add the code to the OnModelCreating 

  1. builder.Entity<ApplicationUser>(b =>  
  2. {  
  3.     b.Property(u => u.Email).HasColumnName("EmailAdress");  
  4. });   

In this code we told entity to get Email property and change this column name to EmailAddress

Now you should make a migration to reflect your changes on Database and we will use it as usual 

Add-Migration command then Update-Database

ASP.NET Core

Now it's time to look in the database

ASP.NET Core

Change Identity table datatype of primary key

We will need to make some changes in ApplicationUser class so go to this class and open it

  ASP.NET Core

  1. public class ApplicationUser : IdentityUser  
  2. {  
  3. }   

ApplicationUser class contains all the custom information I want to store about a user, also inheritance here from IdentityUser gives me all essentials like username ,id, password property or Email, that means if I want to add any property, I will add it here in ApplicationUser class

By default the primary key is string

ASP.NET Core

Because IdentityUser has the primary key  we want to pass our new primary key datatype.

Where will we pass this new datatype? Look to the picture below

ASP.NET Core

As you can see we need to pass our primary key as example int datatype to IdentityUser<int> .

The code will look like this 

  1. public class ApplicationUser : IdentityUser<int>  
  2. {  
  3. }   

And because every user has roles we will need to make this change in roles also by creating new class ApplicationRole which will inherit from IdentityRole class and pass the new datatype to it, and the code will look, as shown below. 

  1. public class ApplicationRole:IdentityRole<int>  
  2. {  
  3. }   

Afterwards, we will make some change in our ApplicationDbContext class in order to open it.

ASP.NET Core

  1. public class ApplicationDbContext : IdentityDbContext<ApplicationUser>   

We need to add our new role and datatype IdentityDbContext to the class. It will look, as shown below.

  1. public class ApplicationDbContext : IdentityDbContext<ApplicationUser,ApplicationRole,int>   

In the last step, we will need to reconfigure ConfigureServices method in the startup class.

We need to make some changes in these lines. 

  1. services.AddIdentity<ApplicationUser, IdentityRole>()  
  2.     .AddEntityFrameworkStores<ApplicationDbContext>()  
  3.     .AddDefaultTokenProviders();  

We need to replace IdentityRole class to our role ApplicationRole class and add our new datatype to AddEntityFrameworkStores<ApplicationDbContext,int>

The code will look, as shown below. 

  1. services.AddIdentity<ApplicationUser, ApplicationRole>()  
  2.     .AddEntityFrameworkStores<ApplicationDbContext,int>()  
  3.     .AddDefaultTokenProviders();   

Now, we have all the required changes achieved but the last step will be the migration.

This time, we have some new things like when you run add-migration command; you will see the warning, as shown below.

ASP.NET Core

When you try to run update-database, you will get an error told to you.

To change the IDENTITY property of a column, the column needs to be dropped and recreated.

Thus, we will drop the database and create it again.

Step 1

Drop the database command to remove the database and press Y. 

Step 2

Remove all the migrations, which you have in Visual Studio.

Step 3

Run add-migration command.

Step 4

Run update-database command.

Now, go to the database to see our changes.

ASP.NET Core

You see now that id has int datatype and not nvarchar.

You can find the code here.

Summary

Now, you can change an Identity column name, Identity table name, Identity primary key. I hope this is helpful to you.

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now