Join Class

Entity framework core identity migrations

In this tutorial you will learn how to execute entity framework migrations command from nuget package manager console, first we learn how to use the command to create database objects for .net core Identity service (authentication service), then we see how to create other custom object from nuget console manager, like we do in code first approach.

ef migrations command

As you can see in above picture, we have created “AuthDb” , also created one folder with name “Migrations” in our project solution explorer, the “migrations” folder contains some files with type of database objects we are going to cerate.

Install Tools for Identity Migrations

Firstly, we need to install following assemblies in nuget package manager, do open your nuget package manager by right clicking on project solution explorer, then search each assembly one by one and install them.


Now before you run migration command from nuget package manager console window, I want make sure you have set that Entity framework DbContext object correctly in Startup configuration file.

In database connection string, instead of using sql authentication, just use windows authentication for this instance. You can write the connection string in appsetting.json file like example below.


Now open your nuget package manager from tools option.

To run any entity framework Migration Commands from Package Manager Console window, first we need to install "dotnet ef" tool for running and managing migrations. Install "dotnet ef" by running the following command on the Package Manager Console window.

Tools => "Nuget Package Manager" => "Package Manager Console"

Install the tool by running following command.

dotnet tool install --global dotnet-ef

Here is the first command to run:

dotnet ef migrations add MyCommand1

The command will create a migration file, which contain table creation script and drop table script for each identity service table (like AspNetRoles, AspNetUsers etc.), below is an example of how the migration script will look like. [generated code by above command].

using System;
using Microsoft.EntityFrameworkCore.Migrations;
public partial class MyCommand1 : Migration
protected override void Up(MigrationBuilder migrationBuilder)
	name: "AspNetRoles",
	columns: table => new
	Id = table.Column<string>(type: "nvarchar(450)", nullable: false),
	Name = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
	NormalizedName = table.Column<string>(type: "nvarchar(256)", maxLength: 256, nullable: true),
	ConcurrencyStamp = table.Column<string>(type: "nvarchar(max)", nullable: true)
	constraints: table =>
	table.PrimaryKey("PK_AspNetRoles", x => x.Id);
protected override void Down(MigrationBuilder migrationBuilder)
        name: "AspNetRoles");

then execute the "InitialCreate" command. The command will add a "Migrations" folder with some scripts in your solution (this part is not necessary).

dotnet ef migrations add InitialCreate
Identity Service Database Tables

Now run the "update" command to cerate database tables, as shown in above picture, this commnd will create database and all database objects like tables, foreign keys etc.

dotnet ef database update

At this point, you should be able to see all database tables have been created in specified database.

There are seven tables for indentity service: AspNetRoles, AspNetUsers, AspNetRoleClaims, AspNetUserClaims, AspNetUserLogins, AspNetUserRoles, AspNetUserTokens

identity service database tables

As you can see in above diagram how all tables relationships are done, “AspNetUsers” is the main table that store records of identity users object.

If you want to generate sql scripts, then the following command will create scripts on same window.

dotnet ef migrations script

the above command will create plain sql scripts with tables and foreign key constrains, this script can be useful when you want to integrate identity service in some existing database, because running migration command on existing database can be little risky, but running the SQL script will be much safe and clean approach.

You may be interested to read following posts

Entity Framework core identity migrations
Learn entity framework orm using c#, entity framework core and earlier version of entity framework, all tutorials are written in c#.
Entity Framework Interview Questions

SQL interview questions
Entity Framework C# Examples | Join .Net C# Course