Entity Framework Migrations

Entity Framework (EF) Migrations is a powerful feature that allows you to evolve your database schema over time as your application's model changes.

What are Migrations?

Migrations are a series of C# classes that represent changes to your database schema. When you make changes to your EF Core model (like adding a new entity, property, or relationship), EF Core can generate a migration that describes how to update your database to match that new model. This ensures your database schema stays synchronized with your application code.

Key Concepts

Common Workflow

Here's the typical process for using EF Core Migrations:

  1. Make Model Changes: Modify your EF Core entity classes or DbContext configuration. For example, add a new property to an existing entity:
    
    public class Product
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public decimal Price { get; set; }
        public string Description { get; set; } // New property
    }
                    
  2. Add a Migration: Scaffold a new migration using the Package Manager Console or the .NET CLI.

    Package Manager Console (Visual Studio)

    Ensure you have the Microsoft.EntityFrameworkCore.Tools NuGet package installed.

    Add-Migration AddProductDescription

    .NET CLI

    Ensure you have the Microsoft.EntityFrameworkCore.Design NuGet package installed.

    dotnet ef migrations add AddProductDescription

    This command generates two files in your Migrations folder:

    • A migration file (e.g., YYYYMMDDHHMMSS_AddProductDescription.cs) containing the Up() and Down() methods.
    • A snapshot file (YourDbContextModelSnapshot.cs) representing the current state of your model.
  3. Review the Migration: Open the generated migration file and inspect the Up() method. EF Core is usually good at generating schema changes, but you might need to manually adjust it, especially for complex scenarios.
    
    public partial class AddProductDescription : Migration
    {
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.AddColumn<string>(
                name: "Description",
                table: "Products",
                type: "nvarchar(max)",
                nullable: true); // Note: nullable might depend on your configuration
        }
    
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropColumn(
                name: "Description",
                table: "Products");
        }
    }
                    
  4. Apply the Migration: Update your database to incorporate the changes defined in the migration.

    Package Manager Console (Visual Studio)

    Update-Database

    .NET CLI

    dotnet ef database update

    This command will execute the Up() method of all pending migrations.

Important: Always review your generated migrations before applying them, especially in production environments. For production deployments, consider using tools like EF Core Power Tools or custom scripts to manage migration application.

Advanced Scenarios

Learn More on Microsoft Docs