dotnet database ai_generated true

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

Also available as: JSON · Markdown
80%Fix Rate
83%Confidence
60Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
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.

generic

Workarounds

  1. 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
  2. 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:

  1. 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

  2. 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

Error Chain

Leads to:
Preceded by:
Frequently confused with: