Migration lock with LockReleaseBehavior.Transaction doesn't prevent concurrent migrations since per-migration transactions
Maintainer thường phản hồi trong vòng 2 ngày
@AndriySvyryd đang làm issue này rồi.
Từ ngày 26/11/2026.
Đánh giá
Issue này chưa được đánh giá.
Mô tả
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:
- For
Transaction, re-read history and re-runPopulateMigrationsinside each transaction after acquiring the lock, and apply only the next migration. This fixes the common case but not the two problems above. - Document
Transactionas unsafe with per-migration transactions, or deprecate it, and steer providers toConnectionorExplicit.
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
- Ngôn ngữ chính
- C#
- Star
- 14.8k
- Fork
- 3.5k
- Merge trung bình
- 3 ngày 7 giờ
- Pull request đã merge (30 ngày)
- 182
Chuẩn bị môi trường
Khởi chạy dev container của dự án ngay trên trình duyệt, bằng tài khoản GitHub của bạn.
- Không có Dockerfile hay tệp Docker Compose
- Có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của dotnet/efcore
-
customer-reported
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
Maintainer thường phản hồi trong vòng 2 ngày
-
GroupBy element `OrderBy(...).First()`/`MaxBy` over an owned type mapped to its own table fails to translateCó thể đã có người làm @cincuranet đã nhận hôm nay. Đang mởarea-groupby area-query
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 25/100
dotnet/efcore#39833 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
SQLite: Polygon.Length translates to GLength, which returns 0Có thể đã có người làm @cincuranet đã nhận hôm nay. Đang mởarea-query area-sqlite
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 45/100
dotnet/efcore#39826 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Querying a TPC base type with a JSON complex property throwsCó thể đã có người làm @cincuranet đã nhận 1 ngày trước. Đang mởarea-query
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 22/100
dotnet/efcore#39814 · 1 người được giao ·
Maintainer thường phản hồi trong vòng 2 ngày
-
Three Cosmos structural-equality tests flaky on Cosmos DB Emulator for WindowsCó thể đã có người làm @su-senka đã nhận 1 ngày trước. Đang mở
Độ khó 3/5 Nửa ngày Mức phù hợp với người mới 35/100
dotnet/efcore#39809 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 2 ngày
Tất cả issue của dotnet/efcore
Issue tương tự
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 77/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
SubtitleEdit/subtitleedit#17247 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Restore test cleanup fails on Windows when restored files are read-onlyCó thể đã có người làm @JamBalaya56562 đã nhận hôm nay. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
duplicati/duplicati#8046 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Bundles Documentation
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
pyrevitlabs/pyRevit#4285 ·
Maintainer thường phản hồi trong vòng 1 ngày