Ef-Core - What regex can I use to replace table names with nolock ones in Db Interceptor

后端 未结 2 1361
广开言路
广开言路 2020-12-10 20:18

I\'ve been trying to port our EF6 project to EF-Core-2.0.

In EF6, we were using DbNolock interceptor for adding With (NOLOCK) hint

2条回答
  •  醉梦人生
    2020-12-10 21:03

    This interception method doesn't look good to me. A better ways IMO is to hook into EF Core infrastructure to replace the IQuerySqlGenerator service implementation for SqlServer with custom implementation overriding the VisitTable method like this:

    public override Expression VisitTable(TableExpression tableExpression)
    {
        // base will append schema, table and alias
        var result = base.VisitTable(tableExpression);
        Sql.Append(" WITH (NOLOCK)");
        return result;
    }
    

    Hooking is a bit complicated because we need to create and replace the "factory" service in order to be able to replace the sql generator. The full code for all that, along with helper extension method is as follows:

    EF Core 3.x:

    using System.Linq.Expressions;
    using Microsoft.EntityFrameworkCore.Query;
    using Microsoft.EntityFrameworkCore.SqlServer.Query.Sql.Internal;
    using Microsoft.EntityFrameworkCore.SqlServer.Query.Internal;
    using Microsoft.EntityFrameworkCore.Query.SqlExpressions;
    
    namespace Microsoft.EntityFrameworkCore
    {
        public static class CustomDbContextOptionsBuilderExtensions
        {
            public static DbContextOptionsBuilder UseCustomSqlServerQuerySqlGenerator(this DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.ReplaceService();
                return optionsBuilder;
            }
        }
    }
    
    namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Sql.Internal
    {
        class CustomSqlServerQuerySqlGeneratorFactory : IQuerySqlGeneratorFactory
        {
            public CustomSqlServerQuerySqlGeneratorFactory(QuerySqlGeneratorDependencies dependencies)
                => Dependencies = dependencies;
            public QuerySqlGeneratorDependencies Dependencies { get; }
            public QuerySqlGenerator Create() => new CustomSqlServerQuerySqlGenerator(Dependencies);
        }
    
        public class CustomSqlServerQuerySqlGenerator : SqlServerQuerySqlGenerator
        {
            public CustomSqlServerQuerySqlGenerator(QuerySqlGeneratorDependencies dependencies)
                : base(dependencies) { }
            protected override Expression VisitTable(TableExpression tableExpression)
            {
                // base will append schema, table and alias
                var result = base.VisitTable(tableExpression);
                Sql.Append(" WITH (NOLOCK)");
                return result;
            }
        }
    }
    

    EF Core 2.x:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Linq.Expressions;
    using Microsoft.EntityFrameworkCore.Query.Expressions;
    using Microsoft.EntityFrameworkCore.Query.Sql;
    using Microsoft.EntityFrameworkCore.Storage;
    using Microsoft.EntityFrameworkCore.SqlServer.Infrastructure.Internal;
    using Microsoft.EntityFrameworkCore.SqlServer.Query.Sql.Internal;
    
    namespace Microsoft.EntityFrameworkCore
    {
        public static class CustomDbContextOptionsBuilderExtensions
        {
            public static DbContextOptionsBuilder UseCustomSqlServerQuerySqlGenerator(this DbContextOptionsBuilder optionsBuilder)
            {
                optionsBuilder.ReplaceService();
                return optionsBuilder;
            }
        }
    }
    
    namespace Microsoft.EntityFrameworkCore.SqlServer.Query.Sql.Internal
    {
        class CustomSqlServerQuerySqlGeneratorFactory : SqlServerQuerySqlGeneratorFactory
        {
            private readonly ISqlServerOptions sqlServerOptions;
            public CustomSqlServerQuerySqlGeneratorFactory(QuerySqlGeneratorDependencies dependencies, ISqlServerOptions sqlServerOptions)
                : base(dependencies, sqlServerOptions) => this.sqlServerOptions = sqlServerOptions;
            public override IQuerySqlGenerator CreateDefault(SelectExpression selectExpression) =>
                new CustomSqlServerQuerySqlGenerator(Dependencies, selectExpression, sqlServerOptions.RowNumberPagingEnabled);
        }
    
        public class CustomSqlServerQuerySqlGenerator : SqlServerQuerySqlGenerator
        {
            public CustomSqlServerQuerySqlGenerator(QuerySqlGeneratorDependencies dependencies, SelectExpression selectExpression, bool rowNumberPagingEnabled)
                : base(dependencies, selectExpression, rowNumberPagingEnabled) { }
            public override Expression VisitTable(TableExpression tableExpression)
            {
                // base will append schema, table and alias
                var result = base.VisitTable(tableExpression);
                Sql.Append(" WITH (NOLOCK)");
                return result;
            }
        }
    }
    

    Quite a bit code for adding just one meaningful line, but the benefit it that it does it the way EF Core would probably do it in case there is such query option.

    Anyway, with the above code all you need is to activate it from your context OnConfiguring override:

    optionsBuilder.UseCustomSqlServerQuerySqlGenerator();
    

提交回复
热议问题