Last active
June 26, 2023 15:13
-
-
Save bdebaere/d8e8fadb116efd717563bd72adacd3f2 to your computer and use it in GitHub Desktop.
Apply WITH(NOLOCK) to every table in EntityFrameworkCore 3.x
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MyDbContext : DbContext | |
{ | |
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | |
{ | |
optionsBuilder.UseSqlServer(connectionString); | |
optionsBuilder.ReplaceService<IQuerySqlGeneratorFactory, WithNolockQuerySqlGeneratorFactory>(); | |
base.OnConfiguring(optionsBuilder); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} | |
} |
Thank you!
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
Sql.Append(@" WITH(NOLOCK)")