ASP.NET Core - Identity Migrations


In this chapter, we will discuss the Identity migration. In ASP.NET Core MVC, authentication and identity features are configured in the Startup.cs file.

public void ConfigureServices(IServiceCollection services) { 
   services.AddIdentity<User, IdentityRole>() 

Anytime you make a change to one of your entity classes or you make a change to your DBContext derived class, chances are you will have to create a new migration script to apply to the database and bring the schema in sync with what is in your code.

This is the case in our application because we now derive our FirstAppDemoDbContext class from the IdentityDbContext class, and it contains its own DbSets, and it will also create a schema to store all the information about the entities that it manages.

using Microsoft.AspNet.Identity.EntityFramework; 
using Microsoft.Data.Entity;  

namespace FirstAppDemo.Models { 
   public class FirstAppDemoDbContext : IdentityDbContext<User> { 
      public DbSet<Employee> Employees { get; set; }  
      protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { 
         optionsBuilder.UseSqlServer("Data Source = (localdb)\\MSSQLLocalDB;
            Initial Catalog = FirstAppDemo;Integrated Security = True;
            Connect Timeout = 30;Encrypt = False;
            TrustServerCertificate = True;ApplicationIntent = ReadWrite;
            MultiSubnetFailover = False"); 

Let us now open the command prompt and make sure we are in the location where the project.json file exists for our project.

Command Prompt

We can also get the Entity Framework commands by typing dnx ef.

Dnx Ef

Our project.json file has a section that maps this “ef” keyword with the EntityFramework.Commands.

"commands": { 
   "web": "Microsoft.AspNet.Server.Kestrel", 
   "ef": "EntityFramework.Commands" 

We can add a migration from here. We also need to provide a name to the migration. Let us use v2 for version 2 and press enter.

V2 For Version

When the migration is complete, you will have a v2 file in your migrations folder.

V2 File

We now want to apply that migration to our database by running the “dnx ef database update” command.

Database Update

The Entity Framework will see there is a migration that needs to be applied and it will execute that migration.

If you come into the SQL Server Object Explorer, you will see the Employee table that we created earlier. You will also see some additional tables that have to store users, claims, roles, and some mapping tables that map users to specific roles.

Additional tables

All these tables are related to the entities that the Identity framework provides.

Let us take a quick look at the users table.

Users Table

You can now see that the columns in the AspNetUsers table include columns to store all those properties that we saw on the Identity User which we inherited from, and its fields like UserName and PasswordHash. So, you have been using some of the built-in Identity services because they also contain an ability to create a user and validate a user's password.