Entity Framework Core Migrations

EF Core Migrations is a feature that enables incremental data schema changes to your database as your model evolves. It is a purely code-based approach to schema management, allowing you to represent your model changes as C# or Visual Basic code.

This documentation covers the core concepts, common scenarios, and best practices for using EF Core Migrations.

Key Concepts

Getting Started with Migrations

1. Enabling Migrations

Before you can use Migrations, you need to enable them for your project. Open the Package Manager Console (Tools > NuGet Package Manager > Package Manager Console) and run the following command:

Enable-Migrations

This command creates a Migrations folder in your project, containing an initial migration file.

2. Creating a New Migration

Whenever you make a change to your EF Core model (e.g., adding a new entity, adding a new property to an entity, or changing relationships), you need to create a new migration to reflect that change in the database.

Use the following command in the Package Manager Console:

Add-Migration 

Replace MigrationName with a descriptive name for your migration (e.g., AddUserTable, UpdateProductPrice).

3. Applying Migrations

Once you have created a migration, you can apply it to your database. This will execute the SQL statements defined in the migration file.

To update your database to the latest migration:

Update-Database

You can also specify a target migration:

Update-Database 

Working with Migration Code

Each migration is a C# (or VB.NET) class that inherits from Migration. It has two main methods:

Example Migration Code


using Microsoft.EntityFrameworkCore.Migrations;

namespace YourProject.Migrations
{
    public partial class AddProductPrice : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<decimal>(
                name: "Price",
                table: "Products",
                nullable: false,
                defaultValue: 0m);
        }

        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "Price",
                table: "Products");
        }
    }
}
            

Advanced Scenarios

Custom SQL

You can execute raw SQL statements within your migrations using migrationBuilder.Sql(...). This is useful for operations that cannot be expressed using the EF Core Migrations API.


migrationBuilder.Sql("CREATE INDEX IX_Customer_LastName ON Customers (LastName);");
            

Conditional Logic

EF Core Migrations can generate different SQL for different database providers. You can also add conditional logic to your migration code if needed.

Data Migrations

To seed or update data as part of your schema changes, you can create "data migrations". This involves overriding the Up and Down methods to include data manipulation operations.

Reverting Migrations

If you need to undo a migration, you can roll back to a specific migration or all migrations.

To revert to a previous migration:

Update-Database 

To revert all migrations (effectively dropping all tracked schema changes):

Update-Database 0

Best Practices