Просмотр исходного кода

!731 多租户切换上下文bug修复
Merge pull request !731 from 拉风的CC/next

zuohuaijun 2 лет назад
Родитель
Сommit
ffd1b74df6
1 измененных файлов с 7 добавлено и 16 удалено
  1. 7 16
      Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs

+ 7 - 16
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs

@@ -39,12 +39,15 @@ public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
         var tenantId = App.GetRequiredService<UserManager>().TenantId;
         var tenantId = App.GetRequiredService<UserManager>().TenantId;
         if (tenantId < 1 || tenantId.ToString() == SqlSugarConst.ConfigId) return;
         if (tenantId < 1 || tenantId.ToString() == SqlSugarConst.ConfigId) return;
 
 
+        var tenant = App.GetRequiredService<SysCacheService>().Get<List<SysTenant>>(CacheConst.KeyTenant)
+               .FirstOrDefault(u => u.Id == tenantId);
+        if (tenant is null || tenant is { TenantType: TenantTypeEnum.Id })
+        {
+            return;
+        }
         // 根据租户Id切库
         // 根据租户Id切库
         if (!iTenant.IsAnyConnection(tenantId.ToString()))
         if (!iTenant.IsAnyConnection(tenantId.ToString()))
         {
         {
-            var tenant = App.GetRequiredService<SysCacheService>().Get<List<SysTenant>>(CacheConst.KeyTenant)
-                .FirstOrDefault(u => u.Id == tenantId);
-
             // 获取主库连接配置
             // 获取主库连接配置
             var dbOptions = App.GetOptions<DbConnectionOptions>();
             var dbOptions = App.GetOptions<DbConnectionOptions>();
             var mainConnConfig = dbOptions.ConnectionConfigs.First(u => u.ConfigId == SqlSugarConst.ConfigId);
             var mainConnConfig = dbOptions.ConnectionConfigs.First(u => u.ConfigId == SqlSugarConst.ConfigId);
@@ -55,20 +58,8 @@ public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
                 ConfigId = tenant.Id,
                 ConfigId = tenant.Id,
                 DbType = tenant.DbType,
                 DbType = tenant.DbType,
                 IsAutoCloseConnection = true,
                 IsAutoCloseConnection = true,
+                ConnectionString = tenant.Connection
             };
             };
-
-            if (tenant.TenantType == TenantTypeEnum.Id)
-            {
-                // 如果是Id隔离,使用默认的连接字符串
-                connectionConfig.ConnectionString = iTenant.GetConnectionScope(SqlSugarConst.ConfigId).CurrentConnectionConfig.ConnectionString;
-                // 继承主库的“启用驼峰转下划线”设置
-                connectionConfig.EnableUnderLine = mainConnConfig.EnableUnderLine;
-            }
-            else
-            {
-                connectionConfig.ConnectionString = tenant.Connection;
-            }
-
             iTenant.AddConnection(connectionConfig);
             iTenant.AddConnection(connectionConfig);
             SqlSugarSetup.SetDbConfig(connectionConfig);
             SqlSugarSetup.SetDbConfig(connectionConfig);
             SqlSugarSetup.SetDbAop(iTenant.GetConnectionScope(tenantId.ToString()));
             SqlSugarSetup.SetDbAop(iTenant.GetConnectionScope(tenantId.ToString()));