EF Core Migrations: Evolve Your Database

Dot NET Programming 13 min min read Updated: Nov 16, 2026 Intermediate
EF Core Migrations: Evolve Your Database
Intermediate Topic 3 of 6

Why Migrations?

Your model changes over time: you add a column, rename a table, add an index. A migration is a small C# file that describes one change in a way EF can apply to the database and also undo. Together they form the history of your schema.

Create the First Migration

bash dotnet ef migrations add InitialCreate dotnet ef database update
CommandWhat it does
migrations add <Name>Compares your model with the last snapshot and creates a migration file
database updateApplies all pending migrations to the database
migrations listShows migrations and whether they were applied
migrations removeDeletes the last migration (if not yet applied)
migrations scriptCreates a SQL script instead of changing the database

A Migrations folder appears with ..._InitialCreate.cs and LibraryContextModelSnapshot.cs.

Change the Model

csharp public class Book { public int Id { get; set; } public required string Title { get; set; } public decimal Price { get; set; } public int Year { get; set; } public string? Isbn { get; set; } // NEW property // ... }
bash dotnet ef migrations add AddBookIsbn dotnet ef database update

The new migration contains an AddColumn operation. Open it and read it every time. It is short and shows exactly what will happen.

Peek Inside a Migration

csharp public partial class AddBookIsbn : Migration { protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn<string>( name: "Isbn", table: "Books", type: "TEXT", nullable: true); } protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropColumn(name: "Isbn", table: "Books"); } }

Go Back

bash dotnet ef database update InitialCreate # roll the database back to that migration dotnet ef migrations remove # then delete the unwanted migration file

Deploying to Production

ApproachNotes
SQL script (recommended)dotnet ef migrations script --idempotent -o update.sql creates a script that can be reviewed and run safely multiple times
Migration bundledotnet ef migrations bundle creates a single executable for your pipeline
Migrate at app startupdb.Database.Migrate(). Convenient for small apps, risky with several servers starting together
Renames: If you rename a property, EF may generate drop column then add column, which loses data. Edit the migration to use RenameColumn before applying it.

Seed Data

csharp protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Author>().HasData( new Author { Id = 1, Name = "Jane Austen" }, new Author { Id = 2, Name = "Mark Twain" }); }
Keep migrations in source control: Commit the Migrations folder with your code so everyone, and the build server, gets the same schema history.

Conclusion

Change the model, run migrations add, read the generated code and apply it with database update. For production prefer reviewed SQL scripts or bundles.

Quick Summary: migrations add creates a step, database update applies it, script is safest for production.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators