You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Need DbCommandInterceptor or nolock extension method for linq on ef core. #12024
public class NoLockInterceptor : DbCommandInterceptor
{
private static readonly Regex _tableAliasRegex =
new Regex(@"(?<tablealias>AS \[Extent\d+\](?! WITH \(NOLOCK\)))",
RegexOptions.Multiline | RegexOptions.IgnoreCase);
[ThreadStatic]
public static bool ApplyNoLock;
public override void ScalarExecuting(DbCommand command,
DbCommandInterceptionContext<object> interceptionContext)
{
if (ApplyNoLock)
{
command.CommandText =
_tableAliasRegex.Replace(command.CommandText,
"${tableAlias} WITH (NOLOCK)");
}
}
public override void ReaderExecuting(DbCommand command,
DbCommandInterceptionContext<dbdatareader> interceptionContext)
{
if (ApplyNoLock)
{
command.CommandText =
_tableAliasRegex.Replace(command.CommandText,
"${tableAlias} WITH (NOLOCK)");
}
}
}
NoLockInterceptor.ApplyNoLock = true;
DbInterception.Add(new NoLockInterceptor());
Or is it possible to add an extension method for linq queries that table-base sql "with nolock" mssql statement?
I don't want change db transaction isolation level this case. Because I don't begin a database transaction for select queries and I want generate a sql query with "with nolock" mssql statement.
@turenc Support for hints is tracked by #6717. The equivalent of the interceptor can be done by hooking into the DiagnosticSource infrastructure. For example, first create an interceptor:
publicclassNoLockInterceptor:IObserver<KeyValuePair<string,object>>{publicvoidOnCompleted(){}publicvoidOnError(Exceptionerror){}publicvoidOnNext(KeyValuePair<string,object>value){if(value.Key==RelationalEventId.CommandExecuting.Name){varcommand=((CommandEventData)value.Value).Command;// Do command.CommandText manipulation here}}}
Next, create a global listener for EF diagnostics. Something like:
As above,if i use master for add operations,but master need to return primary key,that is to say that need to read slave, i use the following way to achieve
@ibrahimozgon I think you will need to look at the kind of queries that you want to change and figure out which kind of pattern you need to match. I'm not sure that there is anything common that can be used as a pivot since EF Core attempts to create more human-readable queries.
Activity
ajcvickers commentedon May 16, 2018
@turenc Support for hints is tracked by #6717. The equivalent of the interceptor can be done by hooking into the DiagnosticSource infrastructure. For example, first create an interceptor:
Next, create a global listener for EF diagnostics. Something like:
And register this as part of application startup:
wangpengxpy commentedon Jul 7, 2018
@ajcvickers use DiagnosticListener implement read/write seperation, for example
As above,if i use master for add operations,but master need to return primary key,that is to say that need to read slave, i use the following way to achieve
but obviously this is not true about above。how to do it at this time?
ajcvickers commentedon Jul 24, 2018
@wangpengxpy I don't really understand your question, but changing the connection in this way is not something I would recommend.
wangpengxpy commentedon Jul 25, 2018
@ajcvickers changing the connection by Database.GetDbConenction??
ibrahimozgon commentedon Oct 30, 2018
Hi, @ajcvickers
I realized that 'AS Extent1' naming convention is changed in EF Core.
Which pattern can I use to do the same?
ajcvickers commentedon Oct 30, 2018
@ibrahimozgon I think you will need to look at the kind of queries that you want to change and figure out which kind of pattern you need to match. I'm not sure that there is anything common that can be used as a pivot since EF Core attempts to create more human-readable queries.