ASP.NET Core Identity Tables Customization With Visual Studio 2017

ASP.NET Core Identity Tables Customization With Visual Studio 2017

CheapASPNETHostingReview.com | Best and cheap ASP.NET Core 1.0 hosting. 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

image001

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.

image002

Select Web Application.

image003

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

image004

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

image005

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.

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

image006

Now, update the database and click enter.

image007

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.

image008

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.

image009

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.

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.

image010

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

image011

Change Identity table property name

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

image012

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

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

image013

Now it’s time to look in the database

image014

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

image015

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

image016

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

image017

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

The code will look like this

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.

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

image018

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

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

We need to make some changes in these lines.

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.

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.

image019

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.

image020

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

Summary

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