Lesson 19 / 26
Migrations
Create and apply schema changes safely with the dotnet ef tool.
Schema as code
A migration is a C# file describing a schema change. You create it from your model, review it, commit it, and apply it to each database. This keeps the schema reproducible across machines.
Add and apply
Install the tool once, add a migration, then update the database. Review the generated file before applying it.
dotnet tool install --global dotnet-ef
dotnet ef migrations add InitialCreate
dotnet ef database updateBe careful in production
Do not call Database.Migrate() at app startup if several instances start together. Generate a SQL script with dotnet ef migrations script and run it as a controlled deployment step.
Quick check: Which command applies pending migrations to a database?
- dotnet ef database update
- dotnet ef migrations add
- dotnet ef dbcontext scaffold
- dotnet build
Answer
dotnet ef database update — `database update` runs migrations that have not yet been applied. `migrations add` only creates the file.