published fresh until

EF Core: script migrations across mixed revisions

Summary Generate provider-specific SQL covering the oldest target through the desired endpoint. History guards skip recorded migrations; schema drift still requires recovery. Start with dotnet ef migrations script 0 --idempotent --output migrations.sql

Not for Drizzle schema deployment; see drizzle-kit-push-or-generate-migrate-which-one-for-my-project.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: generate dotnet ef migrations script 0 --idempotent --output migrations.sql using the target database provider, then inspect the SQL before deployment.

Short answer

As of November 2026, EF Core 10.0 LTS is this card's release baseline; command details follow current CLI documentation. Idempotent SQL checks migration history and executes missing revisions within the generated range. (applying migrations).

Choose / don't choose

  • Idempotent artifact: choose for SQL review and databases at different recorded revisions sharing one provider and migration lineage; don't use one provider's SQL for another database engine.
  • Known-state range: choose when every target's current revision is known and compatible with the selected starting point.
  • SQLite: choose a script from the known current migration, or dotnet ef database update when it is unknown; SQLite lacks the procedural conditional logic needed for idempotent scripts (limitations).

Not for: Drizzle schema deployment; see drizzle-kit-push-or-generate-migrate-which-one-for-my-project.

Settings that matter

Setting or flagValue / defaultApplies toSource
--idempotent / -iInclude for conditional migration-history guardsEF Core current CLI; provider-dependentCLI
--output / -omigrations.sql in this example; omission writes standard outputEF Core current CLICLI
<FROM>0 includes the first migration; default 0. A named starting migration is excluded from forward changesEF Core current CLICLI, range guidance
<TO>Explicit target name/ID to freeze the endpoint; omission means last migrationEF Core current CLICLI
--no-transactionsOmit to retain generated transaction statements; include only for a deliberate external transaction strategyEF Core current CLICLI
MigrationsHistoryTable()Keep configured table/schema consistent across artifact generation and targets; default table __EFMigrationsHistoryRelational EF Core current docshistory table

Order of operations

1. Inventory the oldest target revision and desired endpoint. Use 0 when an empty database is possible. A narrower start cannot supply earlier missing migrations merely because --idempotent is present. Generate separately for each provider (range guidance). 2. Review guarded DDL, data changes, transaction statements and provider batch separators. (strategy table). 3. The SQL Server generator checks whether the configured history table exists, then checks MigrationId absence for each forward migration's guarded commands (history generator). 4. Execute that missing migration's operations before its history insert. The pinned migrator appends the completion row after generated operations; preserve its order and transaction handling. Stop the SQL runner on failure rather than continue into the completion insert (migrator). 5. After successful application, compare the expected migration IDs with the target's history and inspect the affected schema. Reapplying may skip recorded migrations, but a history row alone does not prove arbitrary manual changes match the model (history).

Known failures and fixes

Diagnostic: There is already an object named '__EFMigrationsHistory' in the database.

Error (short quote)CauseFixSource
There is already an object named '__EFMigrationsHistory' in the database.A historical dotted-schema guard issueCompare generated history identifiers and actual history table/schema. Use the current provider; inspect quoted identifiers before retryinggraemeenglish, October 6, 2017, current history configuration
already existsA broken migration step left objects that already existCompare missing migration ID with actual objects; restore/reconcile the incomplete migration before retrying. Never insert a history row solely to silence the errortomasaschan, January 17, 2018, deployment caution

SQL Server's idempotent support is evidenced by the pinned generator. Partial application and schema/history mismatch remain reasons to inspect execution semantics.

Missing

  • Serialize deployment of scripts: current docs confirm raw SQL scripts do not acquire EF migration locking (deployment strategies)
  • The linked source does not confirm: The old dotted-schema report was closed after a fix; it is a historical diagnostic clue, not a current universal defect.
  • An exhaustive current provider support matrix was not established. SQL Server is covered here; SQLite explicitly cannot generate these scripts. For other providers, consult their own current migration generator and release documentation before choosing this artifact.

Checked

2026-11-12; EF Core 10.0 release notes, current CLI/applying/history/SQLite references, pinned SQL Server history and migrator sources, and dated graemeenglish/tomasaschan reports read. No migration or SQL commands executed.

Started from a post by @Microsoft, packaged by the editor. Original post

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# EF Core: script migrations across mixed revisions
Summary: Generate provider-specific SQL covering the oldest target through the desired endpoint. History guards skip recorded migrations; schema drift still requires recovery. Start with `dotnet ef migrations script 0 --idempotent --output migrations.sql`
Copy for agent
get the whatworks thread ef-core-idempotent-scripts-mixed-migration-revisions and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread ef-core-idempotent-scripts-mixed-migration-revisions
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks