AspNetCore.Docs/aspnetcore/includes/scaffold-identity/migrations.md

3.5 KiB

The generated Identity database code requires Entity Framework (EF) Core Migrations. If a migration to generate the Identity schema hasn't been created and applied to the database, create a migration and update the database.

Visual Studio

Visual Studio Connected Services are used to add an EF Core migration and update the database.

In Solution Explorer, double-click Connected Services. In the SQL Server Express LocalDB area of Service Dependencies, select the ellipsis (...) followed by Add migration.

Give the migration a Migration name, such as CreateIdentitySchema, which is a name that describes the migration. Wait for the database context to load in the DbContext class names field, which may take a few seconds. Select Finish to create the migration.

Select the Close button after the operation finishes.

Select the ellipsis (...) again followed by the Update database command.

The Update database with the latest migration dialog opens. Wait for the DbContext class names field to update and for prior migrations to load, which may take a few seconds. Select the Finish button.

Select the Close button after the operation finishes.

The update database command executes the Up method migrations that haven't been applied in a migration code file created by the scaffolder. In this case, the command executes the Up method in the Migrations/{TIME STAMP}_{MIGRATION NAME}.cs file, which creates the Identity tables, constraints, and indexes. The {TIME STAMP} placeholder is a time stamp, and the {MIGRATION NAME} placeholder is the migration name.

.NET CLI

dotnet ef migrations add CreateIdentitySchema
dotnet ef database update

If the Identity schema has already been created but not applied to the database, only the command to update the database must be executed:

Visual Studio

In Solution Explorer, double-click Connected Services. In the SQL Server Express LocalDB area of Service Dependencies, select the ellipsis (...) followed by the Update database command.

The Update database with the latest migration dialog opens. Wait for the DbContext class names field to update and for prior migrations to load, which may take a few seconds. Select the Finish button.

Select the Close button after the operation finishes.

.NET CLI

In a command shell, execute dotnet ef database update:

dotnet ef database update

You can confirm the application of an Identity schema with the following command. The output of the command includes an "applied" column to show which migrations are applied to the database.

Visual Studio

In the Visual Studio Package Manager Console, execute Get-Migration:

Get-Migration

If more than one database context exists, specify the context with the -Context parameter.

.NET CLI

In a command shell, execute dotnet ef migrations list:

dotnet ef migrations list

If more than one database context exists, specify the context with the --context parameter.