多租户全局切库 返回

SqlSugar
3 220

业务场景:由于需要要使用多租户,一个商户建一个数据库,但是我们项目已经开发了90%了,如果使用下面(图1)的方式修改的地方太多了,image.png想请问下有执行前事件,能否执行前切库,也就是一个全局的切库的方法。


图1

image.png

热忱回答3

  • 用的是Furion框架,注入配置

     
     var ConfigList = new ConnectionConfig {
                            ConnectionString = App.Configuration ["ConnectionStrings:DefaultConnection"],//主库
                            DbType = DbType.SqlServer,
                            IsAutoCloseConnection = true,
                            InitKeyType = InitKeyType.Attribute
                      };
    
                      services.AddSqlSugar (ConfigList,
                        db =>
                        {
                              db.Ado.IsDisableMasterSlaveSeparation = true;
                              db.Ado.CommandTimeOut = 30000;//设置超时时间
                              db.Aop.OnLogExecuted = (sql, pars) => //SQL执行完事件
                              {
                                    Console.WriteLine (db.Ado.Connection.ConnectionString);
                                    // LogHelper.WriteLog ($"执行时间:{db.Ado.SqlExecutionTime.TotalMilliseconds}毫秒 \r\nSQL如下:{sql} \r\n参数:{GetParams (pars)} ", "SQL执行");
                              };
                              db.Aop.OnLogExecuting = (sql, pars) => //SQL执行前事件
                              {
                                    Console.WriteLine (db.Ado.Connection.ConnectionString);
                                    if (db.TempItems == null) db.TempItems = new Dictionary<string, object> ();
                              };
                              db.Aop.OnError = (exp) =>//执行SQL 错误事件
                              {
                                    Console.WriteLine (db.Ado.Connection.ConnectionString);
                                    //  LogHelper.WriteLog ($"SQL错误:{exp.Message}\r\nSQL如下:{exp.Sql}", "SQL执行");
    
                                    UnifyContext.Fill (exp.Message);
                                    // throw new Exception (exp.Message);
                              };
                              db.Aop.OnDiffLogEvent = (it) => //可以方便拿到 数据库操作前和操作后的数据变化。
                              {
                                    var editBeforeData = it.BeforeData; //变化前数据
                                    var editAfterData = it.AfterData; //变化后数据
                                    var sql = it.Sql; //SQL
                                    var parameter = it.Parameters; //参数
                                    var data = it.BusinessData; //业务数据
                                    var time = it.Time ?? new TimeSpan (); //时间
                                    var diffType = it.DiffType; //枚举值 insert 、update 和 delete 用来作业务区分
    
                                    //你可以在这里面写日志方法
                                    var log = $"时间:{time.TotalMilliseconds}\r\n";
                                    log += $"类型:{diffType.ToString ()}\r\n";
                                    log += $"SQL:{sql}\r\n";
                                    log += $"参数:{GetParams (parameter)}\r\n";
                                    log += $"业务数据:{JsonConvert.SerializeObject (data)}\r\n";
                                    log += $"变化前数据:{JsonConvert.SerializeObject (editBeforeData)}\r\n";
                                    log += $"变化后数据:{JsonConvert.SerializeObject (editAfterData)}\r\n";
                                    // LogHelper.WriteLog (log, "数据变化前后");
                              };
                        });

     


    0 回复
  • fate sta fate sta VIP0
    2021/11/9

    foreach(var item in ConfigList)

    {

        db.getconnection(item.Configid).Aop事件

    }

    多租户需要这样注册AOP才会全局生效

    0 回复
  • fate sta fate sta VIP0
    2021/11/9

    默认是当前库  db.Aop.OnLogExecuting 

    0 回复