MSDN Documentation

Entity Framework Core Migrations

Migrations are a feature of Entity Framework Core (EF Core) that enables you to incrementally update your database schema to match your data model. They are scriptable, versioned, and can be applied automatically or manually.

What are Migrations?

When you make changes to your EF Core model (e.g., adding a new entity, adding a property to an entity, or changing a data type), your database schema needs to be updated to reflect these changes. Migrations allow you to manage this process safely and reliably.

A migration is essentially a set of instructions that EF Core generates to modify your database. Each migration represents a specific change or a set of related changes to your schema.

Key Concepts

  • Add-Migration: This command creates a new migration file based on the current state of your model and the last applied migration.
  • Update-Database: This command applies pending migrations to your database.
  • Migrations History Table: EF Core creates a special table in your database (usually named __EFMigrationsHistory) to keep track of which migrations have been applied.
  • Code-Based Migrations: Migrations are implemented as C# code, giving you full control over the schema changes.

Getting Started with Migrations

Prerequisites

Ensure you have the necessary EF Core tools installed. You can install them via NuGet Package Manager:

  • Microsoft.EntityFrameworkCore.Tools (for Package Manager Console)
  • dotnet-ef (for .NET CLI)

1. Enable Migrations

If you haven't already, you need to enable migrations for your DbContext. In the Package Manager Console, run:

Add-Migration InitialCreate

Or using the .NET CLI:

dotnet ef migrations add InitialCreate

This command will create a new migration file in a Migrations folder within your project. This first migration will contain code to create the initial database schema based on your current model.

2. Applying Migrations

Once you have created a migration, you can apply it to your database using:

In the Package Manager Console:

Update-Database

Or using the .NET CLI:

dotnet ef database update

This command will execute the SQL scripts defined in the migration files against your target database.

Managing Migrations

Scaffolding Migrations

EF Core can automatically scaffold migrations based on changes detected in your model. After making model changes, you can create a new migration:

Add-Migration AddProductEntity

or

dotnet ef migrations add AddProductEntity

Generating SQL Scripts

You can generate SQL scripts from your migrations without applying them directly to a database:

Package Manager Console:

Script-Migration -From 0 -To latest -OutputMigrationsSql

.NET CLI:

dotnet ef migrations script --output ./migrations.sql

Reverting Migrations

If you need to undo a migration, you can use the Update-Database or database update commands with a specific migration name or target.

Package Manager Console (revert to previous migration):

Update-Database PreviousMigrationName

.NET CLI (revert to previous migration):

dotnet ef database update PreviousMigrationName

Tip

It's generally recommended to review generated migration scripts before applying them to production environments, especially for complex schema changes.

Advanced Migration Scenarios

  • Conditional Migrations: You can write custom logic within migration files to handle specific database features or conditions.
  • Data Seeding: Migrations can be used to seed initial data into your database.
  • Customizing Migration Generation: You can influence how EF Core generates migrations by using data annotations or the Fluent API in your model configuration.

For more detailed information and advanced usage, please refer to the official Entity Framework Core documentation.