The Wild

Apply Migration EF Core: Step-by-Step Guide

· 5 min read · Mammals

In this article
  1. What Are EF Core Migrations?
  2. Prerequisites for Applying Migrations
  3. How to Apply Migrations: Step-by-Step
  4. Applying a Specific Migration
  5. Common Problems and Solutions
  6. Applying Migrations at Runtime
  7. Alternatives to Migrations
  8. Frequently Asked Questions

To apply a migration in EF Core, use the dotnet ef database update command in the terminal (or Update-Database in the Package Manager Console). This command applies pending migrations to your database, creating or updating the schema to match your model.

What Are EF Core Migrations?

Migrations are a feature of Entity Framework Core that lets you incrementally update your database schema to keep it in sync with your application's data model. When you change your C# entity classes (e.g., add a property, change a data type), you create a new migration that contains the necessary operations (like adding a column or creating a table). Applying that migration executes those operations against the database.

Migrations are stored as C# files in your project, typically under a Migrations folder. Each migration has a timestamp-based name and a designer file that holds a snapshot of the model at that point. This snapshot is used to compute the differences for the next migration.

Prerequisites for Applying Migrations

Before you can apply a migration, you need:

  • An EF Core project with a DbContext class and entity models.
  • The EF Core tools installed. For the .NET CLI, run dotnet tool install --global dotnet-ef. For Visual Studio, the Package Manager Console tools are included with the Microsoft.EntityFrameworkCore.Tools package.
  • A configured database provider (e.g., SQL Server, SQLite, PostgreSQL) and a connection string set in your OnConfiguring method or in configuration.
  • An existing migration to apply. If you haven't created one yet, use dotnet ef migrations add InitialCreate.

How to Apply Migrations: Step-by-Step

Here's the typical workflow:

  1. Create a migration (if you haven't already): dotnet ef migrations add MigrationName. This generates the migration files.
  2. Apply the migration to the database: dotnet ef database update. This applies all pending migrations in order.
  3. Verify the schema in your database management tool (e.g., SQL Server Management Studio, Azure Data Studio, or your IDE's database explorer).

If you're using Visual Studio's Package Manager Console, the commands are Add-Migration MigrationName and Update-Database.

Applying a Specific Migration

Sometimes you don't want to apply all migrations up to the latest. You can specify a target migration name:

dotnet ef database update MigrationName

This applies all migrations up to and including the one named MigrationName. If you need to revert to a previous migration, you can specify an earlier migration name, which will downgrade the database schema accordingly.

Common Problems and Solutions

When applying migrations, you might encounter:

  • "No migrations were found" – Ensure you have created a migration first and that your DbContext is correctly configured.
  • "The target database doesn't exist" – EF Core can create the database automatically if the provider supports it (e.g., SQL Server). Otherwise, create it manually or use EnsureCreated() for initial development (but note that EnsureCreated doesn't work with migrations).
  • "Pending model changes" – This error occurs when your model has changed since the last migration. Create a new migration to capture those changes.
  • Connection string issues – Make sure your connection string is correct and that you have permissions to access the database.

Applying Migrations at Runtime

In production, you might need to apply migrations automatically when your application starts. EF Core provides dbContext.Database.Migrate() in C# code. This applies any pending migrations and is often used in startup routines. However, be cautious: running migrations automatically can cause downtime if the schema changes are incompatible with the running application. Many teams prefer to apply migrations as part of a deployment pipeline using the CLI command.

Alternatives to Migrations

If you don't want to use migrations, you have a few options:

  • EnsureCreated() – Creates the database and schema from the model if it doesn't exist. It does not update an existing database and is not recommended for production.
  • SQL scripts – Generate a SQL script from your migrations using dotnet ef migrations script and apply it manually or via a DBA.
  • Third-party tools – Tools like FluentMigrator or DbUp allow you to manage schema changes with your own scripts.

Migrations are the most integrated approach because they track changes automatically and can be versioned with your code.

Frequently Asked Questions

How do I apply migrations in Visual Studio?

Open the Package Manager Console (Tools > NuGet Package Manager > Package Manager Console), select the project with your DbContext, and run Update-Database. If you haven't created a migration yet, run Add-Migration first.

What does "dotnet ef database update" do exactly?

It connects to the database specified in your connection string, checks the migrations table (usually named __EFMigrationsHistory), and applies any migrations that haven't been applied yet, in order. If the database doesn't exist, it will create it (if the provider supports it).

How do I revert a migration?

Use dotnet ef database update PreviousMigrationName. This will apply the down methods of the migrations that are newer than the target, effectively reverting the schema changes. Be aware that this can cause data loss if the down method drops columns or tables.

Can I apply migrations to a database that already has data?

Yes, but you need to be careful. Migrations can add columns with default values, but if you drop or alter columns, you might lose data. Always back up your database before applying migrations to a production database.

Why do I get "The model backing the 'MyContext' context has changed"?

This error occurs when your model has changed since the last migration was created. You need to create a new migration using dotnet ef migrations add to capture those changes, then apply it.

Is it safe to run migrations automatically at startup?

It can be safe for development and small applications, but for production, it's generally better to apply migrations as part of a controlled deployment process. Automatic migrations can cause downtime if the schema change is incompatible with the running version of the app.