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:
- Change Identity table names.
- Change Identity table property names.
- 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
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.
Select Web Application.
Click Change authentication, select an individual user account and click OK, as shown below.
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
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.
"DefaultConnection": "Server=localhost;Database=aspnet-IdentityConfig;user id =sa;password=123456;MultipleActiveResultSets=true"
Now, in Package Manager console, write Add-migration command, followed by the name of this migration command like this and wait for some seconds.
Now, update the database and click enter.
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.
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.
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.
protected override void OnModelCreating(ModelBuilder builder)
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.
Now, refresh the database and you will find the name changed to AspUsers.
Change Identity table property name
We need to change as an example Email to EmailAddress in AspUser table
It’s very similar to what we did before, just we will add the code to the OnModelCreating
b.Property(u => u.Email).HasColumnName("EmailAdress");
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
Now it’s time to look in the database
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
public class ApplicationUser : IdentityUser
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
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
As you can see we need to pass our primary key as example int datatype to IdentityUser<int> .
The code will look like this
public class ApplicationUser : IdentityUser<int>
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.
public class ApplicationRole:IdentityRole<int>
Afterwards, we will make some change in our ApplicationDbContext class in order to open it.
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
We need to add our new role and datatype IdentityDbContext to the class. It will look, as shown below.
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.
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.
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.
Drop the database command to remove the database and press Y.
Remove all the migrations, which you have in Visual Studio.
Run add-migration command.
Run update-database command.
Now, go to the database to see our changes.
You see now that id has int datatype and not nvarchar.
Now, you can change an Identity column name, Identity table name, Identity primary key. I hope this is helpful to you.