Open DvdKhl opened 5 days ago
Note for team: this is assigned to @maumar because Migrations, but it looks directly related to @AndriySvyryd's work in 9. Should we assign issues like this to @AndriySvyryd?
We can assign both and later one will be unassigned.
BTW I think there's similar story with #35133.
Is this a bug or intended behavior of "Protection against concurrent migrations" or "Warn when multiple migration operations can't be run inside a transaction" in this article: https://learn.microsoft.com/en-us/ef/core/what-is-new/ef-core-9.0/whatsnew#migrations?
I am getting the same exception when executing integration test. But I am not using multiple DbContext.
My exception occurs when executing test simultaneously and all of them migrate the database before executing.
@Appli4Ever Concurrent migrations and transactions are two different, unrelated, topics.
My exception occurs when executing test simultaneously and all of them migrate the database before executing.
What exception? Do you see migration lock being taken?
@cincuranet
Message: System.NotSupportedException : User transaction is not supported with a TransactionSuppressed migrations or a retrying execution strategy.
Stack Trace:
MigrationCommandExecutor.ExecuteNonQuery(IReadOnlyList`1 migrationCommands, IRelationalConnection connection, MigrationExecutionState executionState, Boolean commitTransaction, Nullable`1 isolationLevel)
IHistoryRepository.CreateIfNotExists()
<>c.<Migrate>b__20_0(DbContext _, Migrator migrator)
SqlServerExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded)
Migrator.Migrate(String targetMigration)
SqlServerContext`1.InitContext(String connectionString, Boolean useTransaction, String key)
SqlServerContext`1.ctor(String name, String connectionString, Boolean useTransaction, Object constructorParameter, String migrationAssembly)
<>c__DisplayClass0_0`1.<AddContext>b__0(String name, String conn, Boolean useTransaction, Object constructParams)
TestContextFactory`1.Get(TestDbType dbType, String name, String conn, Boolean useTransaction, Object constructorParaemter)
DbContextBuilder`1.Build()
I have to look into it further, maybe this is unrelated but I am getting the same exception as @DvdKhl.
What exception? Do you see migration lock being taken?
Not seeing any migration locks. These should also not occur since im executing the migrations in a System.Threading.Lock
:
private static readonly Lock Sync = new();
private static IDbContextTransaction sharedTransaction;
...
lock (Sync)
{
ctx.Database.UseTransaction(sharedTransaction.GetDbTransaction());
ctx.Database.Migrate();
ctx.Database.BeginTransaction(IsolationLevel.ReadCommitted);
}
(Executing the tests one-by-one works)
One of the migrations contains an operation that cannot be executed in a transaction. It could be a custom operation like
migrationBuilder.Sql("CREATE DATABASE TransactionSuppressed;", suppressTransaction: true)
Or an operation that impacts a memory-optimized table.
In this case you cannot use an external transaction.
@AndriySvyryd I have only 3 auto-generated migrations, looking like this:
Could you suggest which one of those causes this issue?
We use multiple DbContexts within our application, which need to be either all fully migrated or none of them in case of an exception. To achieve this we have surrounded everything in a transaction during the migration.
With EF8 this worked well, but with EF9 we're getting the exception
System.NotSupportedException: 'User transaction is not supported with a TransactionSuppressed migrations or a retrying execution strategy.
'We found the related(?) issue https://github.com/dotnet/efcore/issues/35096 but I can't apply the solution in it since it doesn't fail at a
.Sql()
call. And we're at least not changing any retrying strategy and I'm not sure how to disable it if it is enabled.Minimal repo:
Add the
Microsoft.EntityFrameworkCore.SqlServer
package and notice that this code works in EF8 but throws the exception above in EF9.Stack traces:
Include provider and version information
EF Core version: 9.0.0 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 9.0 Operating system: Win11 23H2 (OS Build 22631.4317) IDE: Visual Studio 2022 17.12.0