Possible breaking change from 5.0.12 to 6.0.1 when using ExecutionStrategy property on RelationalDbContextOptionsBuilder
See original GitHub issueAsk a question
We are using the ExecutionStrategy
property on the RelationalDbContextOptionsBuilder
to decide if we should use a retrying or a non retrying execution strategy. This is decided by evaluating a variable on the CallContext
which is set to true before some special queries where we are using a transaction with the isolation level ReadUncommitted
.
In version 5.0.12 the ExecutionStrategy
gets created, when the transaction is created which allows us to create a non retrying strategy in the above mentioned case but in the last version (6.0.1 and 6.0.0) the ExecutionStrategy
gets created when the DbContext
is created.
This is a problem for us as we are using dependency injection to create the DbContext
and only want to use the isolation level ReadUncommited
for very special queries. These queries then run into an exception when runnning as the execution strategy has already been created.
Include your code
In the dependency injection:
UseSqlServer(connectionString,
sqlServerOptions => {
sqlServerOptions.ExecutionStrategy(dependencies => {
if (DatabaseConfiguration.SuspendExecutionStrategy) {
return new NonRetryingExecutionStrategy(dependencies);
}
return new SqlServerRetryingExecutionStrategy(dependencies);
});
})
when trying an uncommitted read with a transaction
DatabaseConfiguration.SuspendExecutionStrategy = true;
using (var transaction = db.BeginTransaction(IsolationLevel.ReadUncommitted)) {
try {
var result = func(sourceToken); // Do stuff
transaction.Commit();
return result;
} catch (Exception ex) {
transaction?.Rollback();
throw;
}
}
}
DatabaseConfiguration.SuspendExecutionStrategy = false;
This results in an System.InvalidOperationException
with the following message:
The configured execution strategy 'SqlServerRetryingExecutionStrategy' does not support user-initiated transactions. Use the execution strategy returned by 'DbContext.Database.CreateExecutionStrategy()' to execute all the operations in the transaction as a retriable unit.
Include provider and version information
EF Core version: 6.0.0 and 6.0.1 Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET 6.0
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (4 by maintainers)
@TheRealNic Ok, then another alternative is to derive a custom strategy:
Separate to your issue you should wrap
DatabaseConfiguration.SuspendExecutionStrategy = false;
in a try/finally so it’s set back to false when an exception is thrown.