Skip to content

Instantly share code, notes, and snippets.

@bdebaere
Last active June 26, 2023 15:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdebaere/d8e8fadb116efd717563bd72adacd3f2 to your computer and use it in GitHub Desktop.
Save bdebaere/d8e8fadb116efd717563bd72adacd3f2 to your computer and use it in GitHub Desktop.
Apply WITH(NOLOCK) to every table in EntityFrameworkCore 3.x
public class MyDbContext : DbContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(connectionString);
optionsBuilder.ReplaceService<IQuerySqlGeneratorFactory, WithNolockQuerySqlGeneratorFactory>();
base.OnConfiguring(optionsBuilder);
}
}
#pragma warning disable EF1001 // Internal EF Core API usage.
public class WithNolockQuerySqlGenerator : SqlServerQuerySqlGenerator
#pragma warning restore EF1001 // Internal EF Core API usage.
{
/// <summary>
/// Initializes a new instance of the <see cref="WithNolockQuerySqlGenerator"/> class.
/// </summary>
/// <param name="dependencies">The dependencies for this <see cref="WithNolockQuerySqlGenerator"/>.</param>
public WithNolockQuerySqlGenerator(QuerySqlGeneratorDependencies dependencies)
: base(dependencies)
{
}
protected override Expression VisitTable(TableExpression tableExpression)
{
var expression = base.VisitTable(tableExpression);
this.Sql.Append(@" WITH(NOLOCK)");
return expression;
}
}
#pragma warning disable EF1001 // Internal EF Core API usage.
/// <summary>
/// A factory for the <see cref="WithNolockQuerySqlGenerator"/> class.
/// </summary>
public class WithNolockQuerySqlGeneratorFactory : SqlServerQuerySqlGeneratorFactory
#pragma warning restore EF1001 // Internal EF Core API usage.
{
private readonly QuerySqlGeneratorDependencies dependencies;
/// <summary>
/// Initializes a new instance of the <see cref="WithNolockQuerySqlGeneratorFactory"/> class.
/// </summary>
/// <param name="dependencies">The dependencies for this <see cref="WithNolockQuerySqlGeneratorFactory"/>.</param>
public WithNolockQuerySqlGeneratorFactory(QuerySqlGeneratorDependencies dependencies)
: base(dependencies)
{
this.dependencies = dependencies;
}
/// <summary>
/// Creates a default <see cref="WithNolockQuerySqlGenerator"/>.
/// </summary>
/// <returns>The created <see cref="WithNolockQuerySqlGenerator"/>.</returns>
public override QuerySqlGenerator Create()
{
return new WithNolockQuerySqlGenerator(this.dependencies);
}
}
@smitpatel
Copy link

this.VisitSqlFragment(new SqlFragmentExpression(@" WITH(NOLOCK)"));

Sql.Append(@" WITH(NOLOCK)")

@bingzer
Copy link

bingzer commented Mar 30, 2022

Thank you!

@IanKemp
Copy link

IanKemp commented Jun 26, 2023

For future visitors: if you think you need this code, you are wrong. Your actual problem is that your application and/or database is/are poorly designed, or the DB server is under-resourced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment