zoukankan      html  css  js  c++  java
  • 给 EF Core 查询增加 With NoLock

    网上的方案https://blog.csdn.net/sD7O95O/article/details/105320673,有点小问题,正则表达式不支持子查询;

    略作修改。

    using Microsoft.EntityFrameworkCore.Diagnostics;
    using System;
    using System.Collections.Generic;
    using System.Data.Common;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading;
    using System.Threading.Tasks;
    
    
    
    public class QueryWithNoLockDbCommandInterceptor : DbCommandInterceptor
        {
            private static readonly Regex TableAliasRegex =
                new Regex(@"(?<tableAlias>(FROM|JOIN) [[a-zA-Z]w*] AS [[a-zA-Z]w*](?! WITH (NOLOCK)))",
                    RegexOptions.Multiline | RegexOptions.Compiled | RegexOptions.IgnoreCase);
    
            public override InterceptionResult<object> ScalarExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<object> result)
            {
                command.CommandText = TableAliasRegex.Replace(
                    command.CommandText,
                    "${tableAlias} WITH (NOLOCK)"
                    );
                return base.ScalarExecuting(command, eventData, result);
            }
    
            public override ValueTask<InterceptionResult<object>> ScalarExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<object> result,
                CancellationToken cancellationToken = new CancellationToken())
            {
                command.CommandText = TableAliasRegex.Replace(
                    command.CommandText,
                    "${tableAlias} WITH (NOLOCK)"
                    );
                return base.ScalarExecutingAsync(command, eventData, result, cancellationToken);
            }
    
            public override InterceptionResult<DbDataReader> ReaderExecuting(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result)
            {
                command.CommandText = TableAliasRegex.Replace(
                    command.CommandText,
                    "${tableAlias} WITH (NOLOCK)"
                    );
                return result;
            }
    
            public override ValueTask<InterceptionResult<DbDataReader>> ReaderExecutingAsync(DbCommand command, CommandEventData eventData, InterceptionResult<DbDataReader> result,
                CancellationToken cancellationToken = new CancellationToken())
            {
                command.CommandText = TableAliasRegex.Replace(
                    command.CommandText,
                    "${tableAlias} WITH (NOLOCK)"
                    );
                return base.ReaderExecutingAsync(command, eventData, result, cancellationToken);
            }
        }

    dbcontext上加上这个拦截器

    var services = new ServiceCollection();
    services.AddDbContext<TestDbContext>(options =>
    {
        options
            .UseLoggerFactory(loggerFactory)
            .UseSqlServer(DbConnectionString)
            .AddInterceptors(new QueryWithNoLockDbCommandInterceptor())
            ;
    });

    作者:沐雪
    文章均系作者原创或翻译,如有错误不妥之处,欢迎各位批评指正。本文版权归作者和博客园共有,如需转载恳请注明。
    如果您觉得阅读这篇博客让你有所收获,请点击右下方【推荐】
    找一找教程网-随时随地学软件编程 http://www.zyiz.net/

  • 相关阅读:
    mysql 数据库 II(数据类型)
    mysql 数据库 I
    网络协议
    Python 类IV(类成员,异常处理等)
    Python 类III(三大特性,约束,super)
    Python 类II
    类加载机制
    Java新篇章之集合
    Java 类类型之 String 类型
    java 多态
  • 原文地址:https://www.cnblogs.com/puzi0315/p/14900521.html
Copyright © 2011-2022 走看看