# Migrations — .NET Core: Build, Test and Ship Web APIs

Source: https://www.geekswithgeeks.com/en/dotnet-core/ef-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.

```bash
dotnet tool install --global dotnet-ef
dotnet ef migrations add InitialCreate
dotnet ef database update
```

## Be 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.

**Quiz:** Which command applies pending migrations to a database?

- [x] 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.
