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.AddMvc(); 
      services.AddEntityFramework() 
      .AddSqlServer() 
      .AddDbContext<FirstAppDemoDbContext>option.
      UseSqlServer(Configuration["database:connection"]));  
   
   services.AddIdentity<User, IdentityRole>() 
      .AddEntityFrameworkStores<FirstAppDemoDbContext>(); 
}
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.
We can also get the Entity Framework commands by typing 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.
When the migration is complete, you will have a v2 file in your migrations folder.
We now want to apply that migration to our database by running the “dnx ef database update” command.
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.
All these tables are related to the entities that the Identity framework provides.
Let us take a quick look at the 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.

    51 Lectures 
     5.5 hours 

 Anadi Sharma

    44 Lectures 
     4.5 hours 

 Kaushik Roy Chowdhury

    42 Lectures 
     18 hours 

 SHIVPRASAD KOIRALA

    57 Lectures 
     3.5 hours 

 University Code

    40 Lectures 
     2.5 hours 

 University Code

    138 Lectures 
     9 hours 

 Bhrugen Patel
 Print
 Add Notes
 Bookmark this page