Browse Source

feat: 优化多租户Id隔离的连接字符串获取

Id隔离的连接字符串使用主库从配置中读取的连接字符串
许俊杰 3 năm trước cách đây
mục cha
commit
38497f3b6c

+ 14 - 6
Admin.NET/Admin.NET.Core/Service/Tenant/SysTenantService.cs

@@ -18,6 +18,7 @@ public class SysTenantService : IDynamicApiController, ITransient
     private readonly SysRoleMenuService _sysRoleMenuService;
     private readonly SysConfigService _sysConfigService;
     private readonly SysCacheService _sysCacheService;
+    private readonly ISqlSugarClient _sqlSugarClient;
 
     public SysTenantService(SqlSugarRepository<SysTenant> sysTenantRep,
         SqlSugarRepository<SysOrg> sysOrgRep,
@@ -30,7 +31,8 @@ public class SysTenantService : IDynamicApiController, ITransient
         SysUserRoleService sysUserRoleService,
         SysRoleMenuService sysRoleMenuService,
         SysConfigService sysConfigService,
-        SysCacheService sysCacheService)
+        SysCacheService sysCacheService,
+        ISqlSugarClient sqlSugarClient)
     {
         _sysTenantRep = sysTenantRep;
         _sysOrgRep = sysOrgRep;
@@ -44,6 +46,7 @@ public class SysTenantService : IDynamicApiController, ITransient
         _sysRoleMenuService = sysRoleMenuService;
         _sysConfigService = sysConfigService;
         _sysCacheService = sysCacheService;
+        _sqlSugarClient = sqlSugarClient;
     }
 
     /// <summary>
@@ -336,19 +339,24 @@ public class SysTenantService : IDynamicApiController, ITransient
     {
         _sysCacheService.Remove(CacheConst.KeyTenant);
 
+        var iTenant = _sqlSugarClient.AsTenant();
         var tenantList = await _sysTenantRep.GetListAsync();
-        var defautTenant = tenantList.FirstOrDefault(u => u.Id.ToString() == SqlSugarConst.ConfigId);
+        var defaultTenant = tenantList.FirstOrDefault(u => u.Id.ToString() == SqlSugarConst.ConfigId);
         foreach (var tenant in tenantList)
         {
-            if (tenant.Id.ToString() == SqlSugarConst.ConfigId) continue;
+            var tenantId = tenant.Id.ToString();
+            if (tenantId == SqlSugarConst.ConfigId) continue;
 
             // Id模式隔离的租户数据库与主租户一致
             if (tenant.TenantType == TenantTypeEnum.Id)
             {
-                tenant.ConfigId = tenant.Id.ToString();
-                tenant.DbType = defautTenant.DbType;
-                tenant.Connection = defautTenant.Connection;
+                tenant.ConfigId = tenantId;
+                tenant.DbType = defaultTenant.DbType;
+                tenant.Connection = defaultTenant.Connection;
             }
+
+            // 移除 ISqlSugarClient 中的连接
+            iTenant.RemoveConnection(tenantId);
         }
 
         _sysCacheService.Set(CacheConst.KeyTenant, tenantList);

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

@@ -35,11 +35,17 @@ public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
         {
             var tenant = App.GetRequiredService<SysCacheService>().Get<List<SysTenant>>(CacheConst.KeyTenant)
                 .FirstOrDefault(u => u.Id == tenantId);
+
+            var connectionString = tenant.Connection;
+            // 如果是Id隔离,使用默认的连接字符串
+            if (tenant.TenantType == TenantTypeEnum.Id)
+                connectionString = iTenant.GetConnectionScope(SqlSugarConst.ConfigId).CurrentConnectionConfig.ConnectionString;
+
             iTenant.AddConnection(new ConnectionConfig()
             {
                 ConfigId = tenant.Id,
                 DbType = tenant.DbType,
-                ConnectionString = tenant.Connection,
+                ConnectionString = connectionString,
                 IsAutoCloseConnection = true
             });
             SqlSugarSetup.SetDbAop(iTenant.GetConnectionScope(tenantId.ToString()));