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


















