Entity Framework Core Advanced

Applying migrations in production

Running dotnet ef database update from your laptop is fine in development, but production needs a safer, repeatable process. This lesson compares SQL scripts, migration bundles and migrating at startup, and covers backups, review and zero-downtime changes.

Migrations in a pipeline
  1. StartDeveloper adds a migrationReviewed in the pull request
  2. Step 2CI builds a bundle or scriptdotnet ef migrations bundle
  3. Step 3Backup checkedPoint-in-time restore ready
  4. Step 4Release step applies itDeploy-only account with schema rights
  5. EndApp deployedNew code works with the new schema

The options

  • Idempotent SQL script: dotnet ef migrations script --idempotent writes SQL that applies only the missing migrations. A DBA can review it and it runs with any SQL tool.
  • Migration bundle: dotnet ef migrations bundle builds a single executable that applies migrations; ideal for CI/CD pipelines.
  • Migrate at app startup (Database.Migrate()): simple, but several instances may race, the app needs schema-change rights, and a failure stops the app from starting. Avoid it in production.

Generating and running them

Shell
# In CI: create a reviewable script
dotnet ef migrations script --idempotent --output artifacts/migrate.sql

# Or a self-contained bundle
dotnet ef migrations bundle --self-contained -r linux-x64 --output artifacts/efbundle

# In the release step, with a deploy-only connection string from the secret store
./efbundle --connection "$DEPLOY_CONNECTION_STRING"

Safe changes without downtime

During a rolling deploy, old and new versions of the app run at the same time against the same database, so each migration must work with both.

  • Add before you remove: add a new nullable column, deploy code that writes both, backfill, then drop the old column in a later release.
  • Rename in steps (add new, copy, switch code, drop old); a direct rename breaks the running version.
  • Create big indexes ONLINE where your edition supports it, and run long data backfills in batches.
  • Read every generated migration; EF Core may drop and recreate a column when you expected a rename.

Before you run it

  • Take a backup or confirm point-in-time restore is available.
  • Test the script on a copy of production data.
  • Run migrations with a dedicated account that has schema rights; the app account should not.
  • Keep migrations in source control and never edit one that has already run in production.

Interview question

Why is calling Database.Migrate() at application startup risky in production?

Several app instances can start at once and try to apply the same migration concurrently. The app's runtime account then needs rights to change the schema, which breaks least privilege. A failed or slow migration also blocks the app from starting, and there is no chance to review the SQL. Applying migrations as a separate deployment step with a script or bundle avoids these problems.

Your progress is saved only in this browser.

Want the full details? Read the official Microsoft documentation for this topic.