Browse Source

fix: 多租户数据库配置继承主库“启用驼峰转下划线”,以修复ID隔离时多租户表名不一致查询错误问题

许俊杰 3 năm trước cách đây
mục cha
commit
932ca81687

+ 12 - 3
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs

@@ -36,15 +36,24 @@ 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 dbOptions = App.GetOptions<DbConnectionOptions>();
+            var mainConnConfig = dbOptions.ConnectionConfigs.First(u => u.ConfigId == SqlSugarConst.ConfigId);
+
             // 如果是Id隔离,使用默认的连接字符串
             var connectionString = tenant.TenantType == TenantTypeEnum.Id ? iTenant.GetConnectionScope(SqlSugarConst.ConfigId).CurrentConnectionConfig.ConnectionString : tenant.Connection;
-            iTenant.AddConnection(new ConnectionConfig()
+            var connectionConfig = new DbConnectionConfig
             {
                 ConfigId = tenant.Id,
                 DbType = tenant.DbType,
                 ConnectionString = connectionString,
-                IsAutoCloseConnection = true
-            });
+                IsAutoCloseConnection = true,
+
+                // 继承主库的“启用驼峰转下划线”设置
+                EnableUnderLine = mainConnConfig.EnableUnderLine,
+            };
+            iTenant.AddConnection(connectionConfig);
+            SqlSugarSetup.SetDbConfig(connectionConfig);
             SqlSugarSetup.SetDbAop(iTenant.GetConnectionScope(tenantId.ToString()));
         }
         base.Context = iTenant.GetConnectionScope(tenantId.ToString());

+ 1 - 1
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -36,7 +36,7 @@ public static class SqlSugarSetup
     /// 配置连接属性
     /// </summary>
     /// <param name="config"></param>
-    private static void SetDbConfig(DbConnectionConfig config)
+    public static void SetDbConfig(DbConnectionConfig config)
     {
         var configureExternalServices = new ConfigureExternalServices
         {