Entity Framework Core – Migrations

Table of Contents

Why Use Migrations?

Migrations enable you to evolve your database schema safely and consistently across development, testing, and production environments without losing data.

Setup & Prerequisites

Make sure your project references the EF Core tools package.

dotnet add package Microsoft.EntityFrameworkCore.Design

Install the CLI tools (once per machine):

dotnet tool install --global dotnet-ef

Create a Migration

After updating your model, generate a migration with:

dotnet ef migrations add AddBlogUrl

Apply a Migration

Update the database to the latest migration:

dotnet ef database update

Managing Migrations

Advanced Scenarios

Custom SQL in Migrations

migrationBuilder.Sql("UPDATE Blogs SET Url = 'https://example.com' WHERE Url IS NULL");

Generating SQL Scripts

dotnet ef migrations script -o UpdateScript.sql

Great for production deployments where you apply scripts manually.

For more in‑depth examples, see the Advanced EF Core Guide.