English 中文(简体)
ASP.NET Core - Identity Migrations
  • 时间:2025-02-21

ASP.NET Core - Identity Migrations


Previous Page Next Page  

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.

pubpc 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 apppcation 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 { 
   pubpc class FirstAppDemoDbContext : IdentityDbContext<User> { 
      pubpc 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;ApppcationIntent = 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 appped and it will execute that migration.

If you come into the SQL Server Object Explorer, you will see the Employee table that we created earper. 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 pke UserName and PasswordHash. So, you have been using some of the built-in Identity services because they also contain an abipty to create a user and vapdate a user s password.

Advertisements