Build started... Build succeeded. fail: Microsoft.EntityFrameworkCore.Database.Command — Failed executing DbCommand. An error occurred while applying migration 'XXXXXXXX_MigrationName'.
ID: dotnet/ef-migration-failed
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 8 | active | — | — | — |
Root Cause
EF Core migration failures occur when 'dotnet ef database update' cannot apply pending migrations. Causes include connection failures, conflicting schema changes, missing migration files, or model snapshot inconsistencies. Fix by verifying the database connection, resolving conflicts, and regenerating migrations if necessary.
genericWorkarounds
-
85% success Generate a SQL script to diagnose and apply migrations manually
Run 'dotnet ef migrations script --idempotent -o migration.sql' to generate an idempotent SQL script. Review the script to understand what changes will be applied, then execute it against the database. The --idempotent flag ensures already-applied migrations are safely skipped
-
82% success Resolve model snapshot conflicts and create a clean migration
Run 'dotnet ef migrations remove' to remove the last unapplied migration. Fix the conflicting model changes in the DbContext, then run 'dotnet ef migrations add FixedMigration' to create a new clean migration. Verify with 'dotnet ef migrations script' before applying
Dead Ends
Common approaches that don't work:
-
Deleting the Migrations folder and recreating all migrations from scratch
75% fail
Destroys migration history that other environments depend on; production databases expect the existing migration chain. The new initial migration will try to create tables that already exist, causing failures
-
Manually editing the __EFMigrationsHistory table to skip failed migrations
80% fail
Creates a mismatch between the actual database schema and what EF Core thinks the schema is; subsequent migrations will generate incorrect SQL because the model snapshot is out of sync with reality