dotnet / dotnet/efcore

Set isolation level globally for all EF queries/updates/etc.

Open
#31,442 1 comment 1 reaction 0 assignees View on GitHub
area-dbcontext area-query area-save-changes customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

I am checking online what is easiest way to set the transaction isolation level when using EF Core 6.

Some people using https://stackoverflow.com/a/59933774/960567

```C#
public class ConnectionIsolationLevelInterceptor: IDbConnectionInterceptor
{
// Have empty implementations for all unused IDbConnectionInterceptor methods
public void BeganTransaction(DbConnection connection, BeginTransactionInterceptionContext interceptionContext)
{
}

...
... all other unused methods
...

public void Opened(DbConnection connection, DbConnectionInterceptionContext interceptionContext)
{
using (DbCommand command = connection.CreateCommand())
{
command.CommandText = "SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED";
command.ExecuteNonQuery();
}
}
}
```

Some using Migrator,

```
internal sealed class Configuration : DbMigrationsConfiguration
{
public Configuration()
{
SetSqlGenerator("System.Data.SqlClient", new SqlMigrator());
}

private class SqlMigrator : SqlServerMigrationSqlGenerator
{
public override IEnumerable Generate(
IEnumerable migrationOperations, string providerManifestToken)
{
yield return new MigrationStatement { Sql = "set transaction isolation level read committed" };
foreach (var statement in base.Generate(migrationOperations, providerManifestToken))
yield return statement;
}
}
}
```

and some suggested to use base constructor,

```
: base("DefaultConnection", throwIfV1Schema: false)
{
Database.ExecuteSqlCommand("SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;");
}
```

Any guidlines from the team how can we set isolation level globally.

EF Core version:
Database provider: (e.g. Microsoft.EntityFrameworkCore.SqlServer)
Target framework: (e.g. .NET 6.0)
Operating system:
IDE: (e.g. Visual Studio 2022 17.6)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.