liveBook · Manning

Migration

As explained in Building Web APIs with ASP.NET Core

A database migration is a versioned set of changes that updates a database schema or related persisted data to match an application's data model.

A database migration is a versioned set of changes that keeps a database schema aligned with an application's data model while preserving existing data. EF Core compares the current model with the existing database structure and generates a migration containing the required update commands. section 4.3.5

A code-first migration workflow follows these practical steps:

  • Add an initial migration when the database is empty; EF Core creates the migration files and a model snapshot in the /Migrations/ folder. section 4.3.5
  • Apply the migration to create the database schema, using the configured ApplicationDbContext and connection string. section 4.3.5
  • Create later migrations after model changes, then apply them incrementally so new tables and columns appear without discarding existing data. section 4.4.4 section 4.4.5
  • Keep every generated migration file under source control; deleting one can cause update failures such as already-existing tables, columns, or keys. section 9.2.4
> dotnet ef migrations add Initial

Create the initial migration from the project root with the EF Core CLI. section 4.3.5

Applying and reverting changes

After creating a later migration such as Chapter4_Exercises, apply it and inspect the database to confirm the expected tables and columns. To return to the earlier schema, apply the Initial migration and verify that those additions have been removed. section 4.4.5 section 4.4.6

> dotnet ef database update Initial

Apply a named migration to the configured database with the EF Core CLI. section 4.3.5

How Building Web APIs with ASP.NET Core covers it

How deep it goes
Explains generated files, snapshots, creation, application, rollback, verification, and maintenance risks without detailing generated SQL internals.
Examples
Demonstrates Initial, Chapter4_Exercises, and Identity migrations applied, inspected, or used to restore an earlier schema.
What it assumes
Assumes an EF Core model, ApplicationDbContext, and connection string, while introducing migration snapshots and incremental schema updates.
What you can do after
Operate EF Core CLI migrations to create, apply, inspect, and revert database schema changes.
How it connects
Connects EF Core, code-first development, database schemas, data models, ApplicationDbContext, connection strings, and source control.

Also explained in

Compare how 19 books explain migration →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Building Web APIs with ASP.NET Core. The section links point to the parts of the book it draws on.