确认单例模式DisableNvarchar的影响范围 返回

SqlSugar 老数据
11 1384
该叫什么 浮光 发布于2023/2/16
悬赏:5 飞吻
//2. 指定当前方法 (在当前方法加上这行,当前方法都有效)
db.CurrentConnectionConfig.MoreSettings=new ConnMoreSettings(){DisableNvarchar=true};



public static class SqlsugarSetup
{
    public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration, 
    string dbName = "db_master")
    {
        SqlSugarScope sqlSugar = new SqlSugarScope(new ConnectionConfig()
        {
            DbType = SqlSugar.DbType.MySql,
            ConnectionString = configuration.GetConnectionString(dbName),
            IsAutoCloseConnection = true,
        },
            db =>
            {
                //单例参数配置,所有上下文生效
                db.Aop.OnLogExecuting = (sql, pars) =>
                {
                    //Console.WriteLine(sql);//输出sql
                };
                 
                //技巧:拿到非ORM注入对象
                //services.GetService<注入对象>();
            });
        services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
    }
}
//Startup.cs文件添加下面代码
services.AddSqlsugarSetup(Configuration);


版主你好,想请教下我在API中使用如上方式添加Sqlsugar服务,但是需要DisableNvarchar, 请问这个会影响服务中的其他方法吗?

会不会影响所有使用的Sqlsugar的服务呢,麻烦确认下,谢谢!

热忱回答(11)

  • fate sta fate sta VIP0
    2023/2/16

    image.png

    如果想要全局生效应该写在这儿

    0 回复
  • fate sta fate sta VIP0
    2023/2/16

    //如果写在方法里面

    db.CurrentConnectionConfig.MoreSettings=new ConnMoreSettings(){DisableNvarchar=true};

    这个代码只会影响当前请求的当前方法

    0 回复
  • 浮光 浮光 VIP0
    2023/2/16

    @fate sta:好的,谢谢确认, 我只想对当前方法DisableNvarchar, 

    我还以为要在方法结束的时候再写一句db.CurrentConnectionConfig.MoreSettings=new ConnMoreSettings(){DisableNvarchar=false}; 改回来呢!

    0 回复
  • 浮光 浮光 VIP0
    2023/2/24

    @fate sta:版主你好,我使用了DisableNvarchar=true后,短时间内,大概1H 速度确实能和MSSM里一样,非常快,但是1H左右速度就又变得很慢了,具体间隔多长没有统计

    我从Profilter抓了真实Sql, 这个语句在MSSM中几乎是秒查询,但是应用程序里直接飙升到30000ms,用Profilter抓取的也是30000ms

    方案中加With lock, 将匿名改成实体都做了,但是每次只能管一会儿,过了之后又变得特别慢

    0 回复
  • 浮光 浮光 VIP0
    2023/2/24

    @浮光:可以帮忙评估下可能的原因是什么吗

    0 回复
  • fate sta fate sta VIP0
    2023/2/24

    @浮光:你用sqlprofile监控一下SQL时间

    0 回复
  • fate sta fate sta VIP0
    2023/2/24

    看看真实的请求,SQL是不是时间长 (直接在编译器执行是不准的连接池不同)

    0 回复
  • fate sta fate sta VIP0
    2023/2/24

    先确定一点 真实请求过程中慢在哪里

    1. SQL慢

    2. 程序慢

    0 回复
  • 浮光 浮光 VIP0
    2023/2/24

    2DEE5DD0-BDC8-46DA-9D04-7886BFE14B47.png59BD4CEB-44B9-49ED-99F7-324C8E11DFCD.png@fate sta:

    0 回复
  • 浮光 浮光 VIP0
    2023/2/24

    @fate sta:版主你好,这是我抓取的最新的Profiler截图,同一条Sql语句,,我就只是将红色部分注释了,速度就明显提升了但是按照惯例,过一会儿就会又慢下来

    0 回复
  • 浮光 浮光 VIP0
    2023/2/24

    @fate sta:同样的网络,同样的客户端,版主帮忙扫一眼呢,谢谢

    0 回复