Ver código fonte

😁修改多库切换逻辑

zuohuaijun 2 anos atrás
pai
commit
08acc827b6

+ 1 - 1
Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj

@@ -36,7 +36,7 @@
     <PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.9" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="2.32.0" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="2.20.0" />
-    <PackageReference Include="SqlSugarCore" Version="5.1.4.106-Preview20" />
+    <PackageReference Include="SqlSugarCore" Version="5.1.4.106-preview21" />
     <PackageReference Include="System.Linq.Dynamic.Core" Version="1.3.4" />
     <PackageReference Include="UAParser" Version="3.1.47" />
     <PackageReference Include="Yitter.IdGenerator" Version="1.0.14" />

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

@@ -44,7 +44,7 @@ public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
             return;
         }
 
-        // 若当前未登录或是默认租户Id,则返回默认库连接
+        // 若未贴任何表特性或当前未登录或是默认租户Id,则返回默认库连接
         var tenantId = App.GetRequiredService<UserManager>().TenantId;
         if (tenantId < 1 || tenantId.ToString() == SqlSugarConst.MainConfigId) return;
 

+ 38 - 40
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -282,8 +282,8 @@ public static class SqlSugarSetup
             var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false))
                 .WhereIF(config.TableSettings.EnableIncreTable, u => u.IsDefined(typeof(IncreTableAttribute), false)).ToList();
 
-            if (config.ConfigId == SqlSugarConst.MainConfigId) // 默认库
-                entityTypes = entityTypes.Where(u => u.GetCustomAttributes<SysTableAttribute>().Any()).ToList();
+            if (config.ConfigId == SqlSugarConst.MainConfigId) // 默认库(有系统表特性、没有日志表和租户表特性)
+                entityTypes = entityTypes.Where(u => u.GetCustomAttributes<SysTableAttribute>().Any() || (!u.GetCustomAttributes<LogTableAttribute>().Any() && !u.GetCustomAttributes<TenantAttribute>().Any())).ToList();
             else if (config.ConfigId == SqlSugarConst.LogConfigId) // 日志库
                 entityTypes = entityTypes.Where(u => u.GetCustomAttributes<LogTableAttribute>().Any()).ToList();
             else
@@ -303,46 +303,44 @@ public static class SqlSugarSetup
         {
             var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
                 .WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false)).ToList();
-            if (seedDataTypes.Any())
+
+            foreach (var seedType in seedDataTypes)
             {
-                foreach (var seedType in seedDataTypes)
+                var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
+                if (config.ConfigId == SqlSugarConst.MainConfigId) // 默认库(有系统表特性、没有日志表和租户表特性)
                 {
-                    var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
-                    if (config.ConfigId == SqlSugarConst.MainConfigId) // 默认库
-                    {
-                        if (entityType.GetCustomAttribute<SysTableAttribute>() == null)
-                            continue;
-                    }
-                    else if (config.ConfigId == SqlSugarConst.LogConfigId) // 日志库
-                    {
-                        if (entityType.GetCustomAttribute<LogTableAttribute>() == null)
-                            continue;
-                    }
-                    else
-                    {
-                        var att = entityType.GetCustomAttribute<TenantAttribute>(); // 自定义的库
-                        if (att == null || att.configId.ToString() != config.ConfigId) continue;
-                    }
+                    if (entityType.GetCustomAttribute<SysTableAttribute>() == null && (entityType.GetCustomAttribute<LogTableAttribute>() != null || entityType.GetCustomAttribute<TenantAttribute>() != null))
+                        continue;
+                }
+                else if (config.ConfigId == SqlSugarConst.LogConfigId) // 日志库
+                {
+                    if (entityType.GetCustomAttribute<LogTableAttribute>() == null)
+                        continue;
+                }
+                else
+                {
+                    var att = entityType.GetCustomAttribute<TenantAttribute>(); // 自定义的库
+                    if (att == null || att.configId.ToString() != config.ConfigId) continue;
+                }
 
-                    var instance = Activator.CreateInstance(seedType);
-                    var hasDataMethod = seedType.GetMethod("HasData");
-                    var seedData = ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>();
-                    if (seedData == null) continue;
+                var instance = Activator.CreateInstance(seedType);
+                var hasDataMethod = seedType.GetMethod("HasData");
+                var seedData = ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>();
+                if (seedData == null) continue;
 
-                    var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
-                    if (entityInfo.Columns.Any(u => u.IsPrimarykey))
-                    {
-                        // 按主键进行批量增加和更新
-                        var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage();
-                        storage.AsInsertable.ExecuteCommand();
-                        storage.AsUpdateable.ExecuteCommand();
-                    }
-                    else
-                    {
-                        // 无主键则只进行插入
-                        if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any())
-                            dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand();
-                    }
+                var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
+                if (entityInfo.Columns.Any(u => u.IsPrimarykey))
+                {
+                    // 按主键进行批量增加和更新
+                    var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage();
+                    storage.AsInsertable.ExecuteCommand();
+                    storage.AsUpdateable.ExecuteCommand();
+                }
+                else
+                {
+                    // 无主键则只进行插入
+                    if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any())
+                        dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand();
                 }
             }
         }
@@ -362,8 +360,8 @@ public static class SqlSugarSetup
         db.DbMaintenance.CreateDatabase();
 
         // 获取所有系统表-初始化租户库表结构
-        var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
-            && u.IsDefined(typeof(SugarTable), false) && !u.IsDefined(typeof(SysTableAttribute), false)).ToList();
+        var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass &&
+            u.IsDefined(typeof(SugarTable), false) && !u.IsDefined(typeof(SysTableAttribute), false) && !u.IsDefined(typeof(TenantAttribute), false)).ToList();
         if (!entityTypes.Any()) return;
         foreach (var entityType in entityTypes)
         {