Skip to content
Latchkey

dotnet ef migrations: EF Core Migrations in CI

dotnet ef migrations manages EF Core schema migrations, and dotnet ef database update applies them to a database.

EF migrations in CI mean applying or bundling schema changes during deploy. The trick is pointing the tool at the right project and DbContext.

What it does

dotnet ef is a local tool that drives EF Core. migrations add scaffolds a migration, migrations script emits idempotent SQL, database update applies pending migrations, and migrations bundle produces a self-contained executable to apply them at deploy time.

Common usage

Terminal
dotnet tool restore
dotnet ef migrations script -i -o migrate.sql \
  --project src/Data --startup-project src/Api
dotnet ef database update --project src/Data --startup-project src/Api
dotnet ef migrations bundle --self-contained -r linux-x64 -o efbundle

Options

FlagWhat it does
--project <path>Project containing the DbContext and migrations
--startup-project <path>Project with the app host/config used to build the context
-c, --context <name>DbContext to use when more than one exists
-i, --idempotent (script)Generate SQL safe to run against any starting state
-o, --output <file>Output path for script or bundle
--connection <string>Override the connection string

In CI

Prefer migrations script -i or migrations bundle over running database update directly from the build runner; an idempotent SQL script or a bundle decouples schema changes from a live build connection. Restore the tool first with dotnet tool restore. When the context lives in a class library, you must pass both --project and --startup-project.

Common errors in CI

"Unable to create a 'DbContext' of type ..." means the startup project could not be built or its config (connection string) is missing; set --startup-project and the connection. "No project was found. Change the current working directory or use the --project option" speaks for itself. "Could not execute because the specified command or file was not found: dotnet ef" means you skipped dotnet tool restore.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →