querybuilder
querybuilder copied to clipboard
[Table] with nolock for each join tables
How can i add table hint for each join? var query = new Query().FromRaw("Users with (nolock)") .Join("Authors", "Authors.Id", "Posts.AuthorId") .Where("Id", 1) .Where("Status", "Active");
will be:
SELECT * FROM Users with (nolock) INNER JOIN [Authors] ON [Authors].[Id] = [Posts].[AuthorId] WHERE [Id] = 1 AND [Status] = 'Active'
but I want it to be: SELECT * FROM Users with (nolock) INNER JOIN [Authors] with (nolock) ON [Authors].[Id] = [Posts].[AuthorId] WHERE [Id] = 1 AND [Status] = 'Active'
I had the same issue and tackled it by implementing my own compiler.
class NoLockCompiler : SqlServerCompiler
{
public override string CompileTableExpression(SqlResult ctx, AbstractFrom from)
{
return base.CompileTableExpression(ctx, from) + " WITH (NOLOCK)";
}
}
/*
var q = new Query("foo")
.Join("bar as baz", "a", "b");
SELECT * FROM [foo] WITH (NOLOCK) INNER JOIN [bar] as [baz] WITH (NOLOCK) ON [a] = [b]
*/
It might be nice if SqlServerCompiler
had a boolean property to enable this behavior out of the box.