Migrations
Migrations keep your database schema in step with your C# model. Each time you change an entity, you add a migration that describes the change and apply it to the database. This lesson covers the tools, the commands and how to apply migrations safely in production.
- StartChange the modelAdd a property, an entity or a relationship
- Step 2Add a migrationdotnet ef migrations add AddProductStock
- Step 3Review the fileUp() applies the change, Down() reverts it
- Step 4Update the databasedotnet ef database update
- Step 5History is recorded__EFMigrationsHistory lists applied migrations
- EndCommit to source controlTeammates apply the same migrations
The idea in plain words
With code first, your entity classes are the source of truth. When they change, the database has to follow. A migration is a generated C# file that describes the difference between the previous model and the new one, with an Up method to apply it and a Down method to undo it. EF Core stores which migrations have been applied in a table called __EFMigrationsHistory.
Example: commands
Install the tool once per machine, and the design package once per project. In Visual Studio you can use Add-Migration and Update-Database in the Package Manager Console instead.
dotnet tool install --global dotnet-ef
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet ef migrations add InitialCreate # first migration
dotnet ef database update # create or update the database
# later, after adding Product.Stock
dotnet ef migrations add AddProductStock
dotnet ef database update
dotnet ef migrations remove # undo the last migration if not applied
dotnet ef migrations script --idempotent -o migrate.sql # SQL for productionWhat a migration looks like
public partial class AddProductStock : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
name: "Stock", table: "Products", nullable: false, defaultValue: 0);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(name: "Stock", table: "Products");
}
}In Visual Studio 2026
In Visual Studio you run the same commands from the Package Manager Console (Tools, NuGet Package Manager, Package Manager Console). It needs the Microsoft.EntityFrameworkCore.Tools package. Make sure the Default project drop-down shows your web project.
Add-Migration InitialCreate
Update-Database
# later
Add-Migration AddProductStock
Update-Database
Remove-Migration # undo the last unapplied migration
Script-Migration -Idempotent # SQL script for productionCommon mistakes
- Editing the database by hand, so it no longer matches the migrations.
- Not reviewing generated migrations; a rename can appear as drop and add, which loses data.
- Calling Database.Migrate() at startup on several servers at once in production. Prefer an idempotent SQL script or a migration bundle run by your deployment.
- Deleting migration files that have already been applied elsewhere.
Interview question
How do you apply EF Core migrations to a production database?
Generate an idempotent SQL script with dotnet ef migrations script --idempotent, or build a migration bundle with dotnet ef migrations bundle, and run it as a controlled step in the deployment pipeline, ideally after a backup and review. Avoid calling Database.Migrate() at application startup in multi-instance production environments because concurrent instances can race and the app account would need schema permissions.
Want the full details? Read the official Microsoft documentation for this topic.