多租户注入

1、注入

请:升级到1.7

需要设置ConfigId来区分是哪个连接对象

  //可以循环 
  services.AddSqlSugar(new IocConfig()
  {              ConfigId="1",
                ConnectionString = "连接字符串1",
                DbType = IocDbType.MySql,
                IsAutoCloseConnection = true
   });
  services.AddSqlSugar(new IocConfig()
  {             ConfigId="2",
                ConnectionString = "连接字符串2",
                DbType = IocDbType.SqlServer,
                IsAutoCloseConnection = true
   });
   
   
  //AOP 统一配置  禁止循环,只能声名一次
  services.ConfigurationSugar(db =>  
  {
        //里面可以循环
       db.GetConnection("1").Aop.OnLogExecuting = (sql, p) =>
       {
          Console.WriteLine(sql);
       };
       db.GetConnection("2").Aop.OnLogExecuting = (sql, p) =>
       {
          Console.WriteLine(sql);
       };
  });


2、用法

    try
     {
         
        var mysqldb=DbScoped.SugarScope.GetConnection("1");//声名到事务外面
        var sqlserverdb=DbScoped.SugarScope.GetConnection("2");
      
        DbScoped.SugarScope.BeginTran();//事务用父级对象
 
        
        mysqldb.Insertable(data).ExecuteCommand();  //操作数据库用子db
        sqlserverdb.Insertable(data).ExecuteCommand();    
 
        DbScoped.SugarScope.CommitTran();
     }
     catch (Exception ex)
     {
        DbScoped.SugarScope.RollbackTran();                  
        throw;
     }


果糖网