Migration lock with LockReleaseBehavior.Transaction doesn't prevent concurrent migrations since per-migration transactions

オープン
#39,824 コメント 0 件 リアクション 0 件 担当者 1 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@AndriySvyryd がすでに取り組んでいます。

2026年11月26日 から。

評価

この issue はまだ評価されていません。

説明

area-migrations
Bug description

Since EF 10 (dotnet/efcore#36662, which went back to one transaction per migration), the migration lock no longer prevents concurrent Migrate() calls from applying the same migration twice when the provider uses LockReleaseBehavior.Transaction.

Migrator.MigrateImplementation computes the plan once, via PopulateMigrations(GetAppliedMigrations()), and then runs each migration in its own transaction. MigrationCommandExecutor reacquires the lock when each new transaction starts (ReacquireIfNeeded(..., transactionRestarted: true)), but the applied migrations are never re-read after the lock is reacquired. Two instances can therefore both read history, then take turns under the lock, and each applies the full stale plan. With EF 9's single transaction this couldn't happen.

This was reported against Npgsql in npgsql/efcore.pg#4162, and it reproduces reliably with several parallel Migrate() calls. The failure is 42701: column ... already exists. Npgsql is moving to a session-level advisory lock (LockReleaseBehavior.Connection) in npgsql/efcore.pg#4163. However, other providers that use LockReleaseBehavior.Transaction are probably affected, e.g. the YDB provider.

Two related problems with Transaction, both independent of the stale plan:

  • After a transaction-suppressed command, EF calls ReacquireIfNeeded(..., transactionRestarted: null), so suppressed commands run without any lock.
  • A migration containing a transaction-suppressed command is split over several transactions, and its history row is only written in the last one. Even if the plan were re-read in each transaction, another instance could start the same migration in between. This doesn't affect migrations with only transactional commands.

Possible directions:

  1. For Transaction, re-read history and re-run PopulateMigrations inside each transaction after acquiring the lock, and apply only the next migration. This fixes the common case but not the two problems above.
  2. Document Transaction as unsafe with per-migration transactions, or deprecate it, and steer providers to Connection or Explicit.
Your code
// Several app instances (or parallel tasks) on a database with pending migrations:
await Task.WhenAll(Enumerable.Range(0, 5).Select(async _ =>
{
    await using var ctx = new MyContext();
    await ctx.Database.MigrateAsync();
}));

Against PostgreSQL with Npgsql 10/11, this intermittently fails with 42701: column "..." of relation "..." already exists. See npgsql/efcore.pg#4162 for a full repro.

Stack traces

N/A

Verbose output

N/A

EF Core version

10.0, 11.0

Database provider

Any provider using LockReleaseBehavior.Transaction (seen with Npgsql.EntityFrameworkCore.PostgreSQL)

Target framework

.NET 10 / .NET 11

Operating system

Any

IDE

N/A

主要言語
C#
スター
14.8k
フォーク
3.5k
平均マージ
3日 7時間
マージ済み PR(30日)
182

環境構築

Codespaces で開く

このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

dotnet/efcore のほかの issue

dotnet/efcore の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。