Procházet zdrojové kódy

调整优化多库模式

zuohuaijun před 4 roky
rodič
revize
3bf216b601
43 změnil soubory, kde provedl 52 přidání a 342 odebrání
  1. 10 5
      Admin.NET/Admin.NET.Application/AppConfig.json
  2. 1 1
      Admin.NET/Admin.NET.Application/Const/TestConst.cs
  3. 1 1
      Admin.NET/Admin.NET.Application/Entity/Student.cs
  4. 4 2
      Admin.NET/Admin.NET.Application/Entity/Test.cs
  5. 1 1
      Admin.NET/Admin.NET.Application/Entity/TreeTest.cs
  6. 1 1
      Admin.NET/Admin.NET.Application/Filter/DataEntityFilter.cs
  7. 1 1
      Admin.NET/Admin.NET.Application/SeedData/TestSeedData.cs
  8. 1 1
      Admin.NET/Admin.NET.Core/Admin.NET.Core.csproj
  9. 6 92
      Admin.NET/Admin.NET.Core/Admin.NET.Core.xml
  10. 0 45
      Admin.NET/Admin.NET.Core/Attribute/SqlSugarEntityAttribute.cs
  11. 0 5
      Admin.NET/Admin.NET.Core/Const/SqlSugarConst.cs
  12. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysCodeGen.cs
  13. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysCodeGenConfig.cs
  14. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysConfig.cs
  15. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysDataResource.cs
  16. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysDictData.cs
  17. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysDictType.cs
  18. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysFile.cs
  19. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysLogAudit.cs
  20. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysLogDiff.cs
  21. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysLogEx.cs
  22. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysLogOp.cs
  23. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysLogVis.cs
  24. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysMenu.cs
  25. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysOrg.cs
  26. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysPos.cs
  27. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysRole.cs
  28. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysRoleMenu.cs
  29. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysRoleOrg.cs
  30. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysTenant.cs
  31. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysTimer.cs
  32. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysUser.cs
  33. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysUserExtOrgPos.cs
  34. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysUserOrg.cs
  35. 0 1
      Admin.NET/Admin.NET.Core/Entity/SysUserRole.cs
  36. 0 1
      Admin.NET/Admin.NET.Core/Entity/WechatPay.cs
  37. 0 1
      Admin.NET/Admin.NET.Core/Entity/WechatUser.cs
  38. 0 10
      Admin.NET/Admin.NET.Core/Extension/ObjectExtension.cs
  39. 1 1
      Admin.NET/Admin.NET.Core/Extension/RepositoryExtension.cs
  40. 2 44
      Admin.NET/Admin.NET.Core/Option/ConnectionStringsOptions.cs
  41. 4 26
      Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs
  42. 18 78
      Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs
  43. 1 2
      Admin.NET/Admin.NET.Core/Util/CodeGenUtil.cs

+ 10 - 5
Admin.NET/Admin.NET.Application/AppConfig.json

@@ -1,14 +1,19 @@
 {
   "ConnectionStrings": {
-    "DefaultConnection": "DataSource=C:\\db\\Admin.NET.db",
-    "DefaultDbType": "Sqlite", // MySql、SqlServer、Sqlite、Oracle、PostgreSQL、Dm、Kdbndp、Oscar、MySqlConnector、Access
     "EnableInitTable": true, // 启用初始化库表
     "EnableDiffLog": false, // 启用库表差异日志
-    "DbConfigs": [
+    "ConnectionConfigs": [ // 默认第一个为主库
       {
-        "DbConfigId": "Test",
+        "ConfigId": "Dilon",
+        "DbType": "Sqlite", // MySql、SqlServer、Sqlite、Oracle、PostgreSQL、Dm、Kdbndp、Oscar、MySqlConnector、Access
+        "ConnectionString": "DataSource=./Admin.NET.db", // DataSource=C:\\db\\Admin.NET.db
+        "IsAutoCloseConnection": true
+      },
+      {
+        "ConfigId": "Test",
         "DbType": "Sqlite",
-        "DbConnection": "DataSource=C:\\db\\Test.db"
+        "ConnectionString": "DataSource=./Test.db",
+        "IsAutoCloseConnection": true
       }
     ]
   },

+ 1 - 1
Admin.NET/Admin.NET.Application/Const/TestConst.cs

@@ -8,7 +8,7 @@ public class TestConst
     public const string GroupName = "我的业务";
 
     /// <summary>
-    /// 自己业务数据库标识
+    /// 数据库标识
     /// </summary>
     public const string ConfigId = "Test";
 }

+ 1 - 1
Admin.NET/Admin.NET.Application/Entity/Student.cs

@@ -6,7 +6,7 @@ namespace Admin.NET.Application.Entity;
 /// 多库代码生成测试学生表
 /// </summary>
 [SugarTable("d_student", "多库代码生成测试学生表")]
-[SqlSugarEntity(DbConfigId = TestConst.ConfigId)]
+[Tenant(TestConst.ConfigId)]
 public class Student : EntityBase
 {
     /// <summary>

+ 4 - 2
Admin.NET/Admin.NET.Application/Entity/Test.cs

@@ -1,10 +1,12 @@
-namespace Admin.NET.Application.Entity;
+using Admin.NET.Application.Const;
+
+namespace Admin.NET.Application.Entity;
 
 /// <summary>
 /// 自己业务数据表
 /// </summary>
 [SugarTable("d_test", "自己业务数据表")]
-[SqlSugarEntity] // [SqlSugarEntity(DbConfigId = TestConst.ConfigId)]
+[Tenant(TestConst.ConfigId)]
 public class Test : EntityBase
 {
     /// <summary>

+ 1 - 1
Admin.NET/Admin.NET.Application/Entity/TreeTest.cs

@@ -6,7 +6,7 @@ namespace Admin.NET.Application.Entity;
 /// 多库代码生成树形测试表
 /// </summary>
 [SugarTable("d_treetest", "多库代码生成树形测试表")]
-[SqlSugarEntity(DbConfigId = TestConst.ConfigId)]
+[Tenant(TestConst.ConfigId)]
 public class TreeTest : EntityBase
 {
     /// <summary>

+ 1 - 1
Admin.NET/Admin.NET.Application/Filter/DataEntityFilter.cs

@@ -1,7 +1,7 @@
 namespace Admin.NET.Application.Filter;
 
 /// <summary>
-/// 自定义业务实体过滤器(XXX数据)
+/// 实体过滤器
 /// </summary>
 public class DataEntityFilter : IEntityFilter
 {

+ 1 - 1
Admin.NET/Admin.NET.Application/SeedData/TestSeedData.cs

@@ -1,7 +1,7 @@
 namespace Admin.NET.Application.SeedData;
 
 /// <summary>
-/// 自己业务表种子数据
+/// 实体种子数据
 /// </summary>
 public class TestSeedData : ISqlSugarEntitySeedData<Test>
 {

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

@@ -29,7 +29,7 @@
     <PackageReference Include="OnceMi.AspNetCore.OSS" Version="1.1.5" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.Api" Version="2.14.0" />
     <PackageReference Include="SKIT.FlurlHttpClient.Wechat.TenpayV3" Version="2.9.1" />
-    <PackageReference Include="SqlSugarCore" Version="5.0.8.6" />
+    <PackageReference Include="SqlSugarCore" Version="5.0.8.7" />
     <PackageReference Include="System.Linq.Dynamic.Core" Version="1.2.18" />
     <PackageReference Include="UAParser" Version="3.1.47" />
     <PackageReference Include="Yitter.IdGenerator" Version="1.0.12" />

+ 6 - 92
Admin.NET/Admin.NET.Core/Admin.NET.Core.xml

@@ -19,39 +19,6 @@
             非实体表特性
             </summary>
         </member>
-        <member name="T:Admin.NET.Core.SqlSugarEntityAttribute">
-            <summary>
-            SqlSugar实体配置特性
-            </summary>
-        </member>
-        <member name="P:Admin.NET.Core.SqlSugarEntityAttribute.DbConfigId">
-            <summary>
-            数据库上下文标识
-            </summary>
-        </member>
-        <member name="P:Admin.NET.Core.SqlSugarEntityAttribute.Order">
-            <summary>
-            实体顺序
-            </summary>
-        </member>
-        <member name="M:Admin.NET.Core.SqlSugarEntityAttribute.#ctor">
-            <summary>
-            默认配置
-            </summary>
-        </member>
-        <member name="M:Admin.NET.Core.SqlSugarEntityAttribute.#ctor(System.Int32)">
-            <summary>
-            配置实体排序
-            </summary>
-            <param name="order"></param>
-        </member>
-        <member name="M:Admin.NET.Core.SqlSugarEntityAttribute.#ctor(System.Int32,System.String)">
-            <summary>
-            配置数据库标识和实体排序
-            </summary>
-            <param name="order"></param>
-            <param name="dbConfigId"></param>
-        </member>
         <member name="T:Admin.NET.Core.SqlSugarUnitOfWorkAttribute">
             <summary>
             SqlSugar工作单元配置特性
@@ -2560,13 +2527,6 @@
             对象拓展类
             </summary>
         </member>
-        <member name="M:Admin.NET.Core.ObjectExtension.GetSqlSugarEntityOrder(System.Type)">
-            <summary>
-            获取实体排序
-            </summary>
-            <param name="type">排序类型</param>
-            <returns>int</returns>
-        </member>
         <member name="M:Admin.NET.Core.ObjectExtension.HasImplementedRawGeneric(System.Type,System.Type)">
             <summary>
             判断类型是否实现某个泛型
@@ -2788,21 +2748,6 @@
             数据库链接配置
             </summary>
         </member>
-        <member name="P:Admin.NET.Core.ConnectionStringsOptions.DefaultConfigId">
-            <summary>
-            默认数据库标识
-            </summary>
-        </member>
-        <member name="P:Admin.NET.Core.ConnectionStringsOptions.DefaultDbType">
-            <summary>
-            默认数据库类型
-            </summary>
-        </member>
-        <member name="P:Admin.NET.Core.ConnectionStringsOptions.DefaultConnection">
-            <summary>
-            默认数据库连接字符串
-            </summary>
-        </member>
         <member name="P:Admin.NET.Core.ConnectionStringsOptions.EnableInitTable">
             <summary>
             启用初始化库表
@@ -2813,34 +2758,9 @@
             启用库表差异日志
             </summary>
         </member>
-        <member name="P:Admin.NET.Core.ConnectionStringsOptions.DbConfigs">
-            <summary>
-            业务库集合
-            </summary>
-        </member>
-        <member name="T:Admin.NET.Core.DbConfig">
-            <summary>
-            数据库参数
-            </summary>
-        </member>
-        <member name="P:Admin.NET.Core.DbConfig.DbConfigId">
-            <summary>
-            数据库编号
-            </summary>
-        </member>
-        <member name="P:Admin.NET.Core.DbConfig.DbType">
+        <member name="P:Admin.NET.Core.ConnectionStringsOptions.ConnectionConfigs">
             <summary>
-            数据库类型
-            </summary>
-        </member>
-        <member name="P:Admin.NET.Core.DbConfig.DbConnection">
-            <summary>
-            数据库连接字符串
-            </summary>
-        </member>
-        <member name="P:Admin.NET.Core.DbConfig.EnableInitTable">
-            <summary>
-            启用初始化库表
+            数据库配置集合
             </summary>
         </member>
         <member name="T:Admin.NET.Core.OSSProviderOptions">
@@ -6392,32 +6312,26 @@
             初始化数据库结构
             </summary>
         </member>
-        <member name="M:Admin.NET.Core.SqlSugarSetup.SetDeletedEntityFilter(SqlSugar.SqlSugarProvider)">
+        <member name="M:Admin.NET.Core.SqlSugarSetup.SetDeletedEntityFilter(SqlSugar.SqlSugarScopeProvider)">
             <summary>
             配置实体假删除过滤器
             </summary>
         </member>
-        <member name="M:Admin.NET.Core.SqlSugarSetup.SetOrgEntityFilter(SqlSugar.SqlSugarProvider)">
+        <member name="M:Admin.NET.Core.SqlSugarSetup.SetOrgEntityFilter(SqlSugar.SqlSugarScopeProvider)">
             <summary>
             配置实体机构过滤器
             </summary>
         </member>
-        <member name="M:Admin.NET.Core.SqlSugarSetup.SetCustomEntityFilter(SqlSugar.SqlSugarProvider)">
+        <member name="M:Admin.NET.Core.SqlSugarSetup.SetCustomEntityFilter(SqlSugar.SqlSugarScopeProvider)">
             <summary>
             配置自定义实体过滤器
             </summary>
         </member>
-        <member name="M:Admin.NET.Core.SqlSugarSetup.SetTenantEntityFilter(SqlSugar.SqlSugarProvider)">
+        <member name="M:Admin.NET.Core.SqlSugarSetup.SetTenantEntityFilter(SqlSugar.SqlSugarScopeProvider)">
             <summary>
             配置租户实体过滤器
             </summary>
         </member>
-        <member name="M:Admin.NET.Core.SqlSugarSetup.DealConnectionStr(Admin.NET.Core.ConnectionStringsOptions@)">
-            <summary>
-            处理本地库根目录路径
-            </summary>
-            <param name="dbOptions"></param>
-        </member>
         <member name="T:Admin.NET.Core.AdminResultProvider">
             <summary>
             全局规范化结果

+ 0 - 45
Admin.NET/Admin.NET.Core/Attribute/SqlSugarEntityAttribute.cs

@@ -1,45 +0,0 @@
-namespace Admin.NET.Core;
-
-/// <summary>
-/// SqlSugar实体配置特性
-/// </summary>
-[SuppressSniffer, AttributeUsage(AttributeTargets.Class)]
-public class SqlSugarEntityAttribute : Attribute
-{
-    /// <summary>
-    /// 数据库上下文标识
-    /// </summary>
-    public string DbConfigId { get; set; } = SqlSugarConst.ConfigId;
-
-    /// <summary>
-    /// 实体顺序
-    /// </summary>
-    public int Order { get; set; }
-
-    /// <summary>
-    /// 默认配置
-    /// </summary>
-    public SqlSugarEntityAttribute()
-    {
-    }
-
-    /// <summary>
-    /// 配置实体排序
-    /// </summary>
-    /// <param name="order"></param>
-    public SqlSugarEntityAttribute(int order)
-    {
-        Order = order;
-    }
-
-    /// <summary>
-    /// 配置数据库标识和实体排序
-    /// </summary>
-    /// <param name="order"></param>
-    /// <param name="dbConfigId"></param>
-    public SqlSugarEntityAttribute(int order, string dbConfigId)
-    {
-        Order = order;
-        DbConfigId = dbConfigId;
-    }
-}

+ 0 - 5
Admin.NET/Admin.NET.Core/Const/SqlSugarConst.cs

@@ -19,9 +19,4 @@ public class SqlSugarConst
     /// SqlSugar数据库链接集合(多库代码生成用)
     /// </summary>
     public static List<ConnectionConfig> ConnectionConfigs = new();
-
-    /// <summary>
-    /// 默认库名  DefaultConnection 对应的库名
-    /// </summary>
-    public const string CONFIG_DEFAULT_DB = "Dilon";
 }

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysCodeGen.cs

@@ -4,7 +4,6 @@
 /// 代码生成表
 /// </summary>
 [SugarTable("sys_code_gen", "代码生成表")]
-[SqlSugarEntity]
 public class SysCodeGen : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysCodeGenConfig.cs

@@ -4,7 +4,6 @@
 /// 代码生成字段配置表
 /// </summary>
 [SugarTable("sys_code_gen_config", "代码生成字段配置表")]
-[SqlSugarEntity]
 public class SysCodeGenConfig : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysConfig.cs

@@ -4,7 +4,6 @@
 /// 系统参数配置表
 /// </summary>
 [SugarTable("sys_config", "系统参数配置表")]
-[SqlSugarEntity]
 public class SysConfig : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysDataResource.cs

@@ -4,7 +4,6 @@
 ///  数据资源表
 ///</summary>
 [SugarTable("sys_data_resource", "数据资源表")]
-[SqlSugarEntity]
 public class SysDataResource : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysDictData.cs

@@ -4,7 +4,6 @@
 /// 系统字典值表
 /// </summary>
 [SugarTable("sys_dict_data", "系统字典值表")]
-[SqlSugarEntity]
 public class SysDictData : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysDictType.cs

@@ -4,7 +4,6 @@
 /// 系统字典类型表
 /// </summary>
 [SugarTable("sys_dict_type", "系统字典类型表")]
-[SqlSugarEntity]
 public class SysDictType : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysFile.cs

@@ -4,7 +4,6 @@
 /// 系统文件表
 /// </summary>
 [SugarTable("sys_file", "系统文件表")]
-[SqlSugarEntity]
 public class SysFile : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysLogAudit.cs

@@ -4,7 +4,6 @@
 /// 系统审计日志表
 /// </summary>
 [SugarTable("sys_log_audit", "系统审计日志表")]
-[SqlSugarEntity]
 public class SysLogAudit : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysLogDiff.cs

@@ -4,7 +4,6 @@
 /// 系统差异日志表
 /// </summary>
 [SugarTable("sys_log_diff", "系统差异日志表")]
-[SqlSugarEntity]
 public class SysLogDiff : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysLogEx.cs

@@ -4,7 +4,6 @@
 /// 系统异常日志表
 /// </summary>
 [SugarTable("sys_log_ex", "系统异常日志表")]
-[SqlSugarEntity]
 public class SysLogEx : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysLogOp.cs

@@ -4,7 +4,6 @@
 /// 系统操作日志表
 /// </summary>
 [SugarTable("sys_log_op", "系统操作日志表")]
-[SqlSugarEntity]
 public class SysLogOp : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysLogVis.cs

@@ -4,7 +4,6 @@
 /// 系统访问日志表
 /// </summary>
 [SugarTable("sys_log_vis", "系统访问日志表")]
-[SqlSugarEntity]
 public class SysLogVis : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysMenu.cs

@@ -4,7 +4,6 @@
 /// 系统菜单表
 /// </summary>
 [SugarTable("sys_menu", "系统菜单表")]
-[SqlSugarEntity]
 public class SysMenu : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysOrg.cs

@@ -4,7 +4,6 @@
 /// 系统机构表
 /// </summary>
 [SugarTable("sys_org", "系统机构表")]
-[SqlSugarEntity]
 public class SysOrg : EntityTenant
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysPos.cs

@@ -4,7 +4,6 @@
 /// 系统职位表
 /// </summary>
 [SugarTable("sys_pos", "系统职位表")]
-[SqlSugarEntity]
 public class SysPos : EntityTenant
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysRole.cs

@@ -4,7 +4,6 @@
 /// 系统角色表
 /// </summary>
 [SugarTable("sys_role", "系统角色表")]
-[SqlSugarEntity]
 public class SysRole : EntityTenant
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysRoleMenu.cs

@@ -4,7 +4,6 @@
 /// 系统角色菜单表
 /// </summary>
 [SugarTable("sys_role_menu", "系统角色菜单表")]
-[SqlSugarEntity]
 public class SysRoleMenu : EntityBaseId
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysRoleOrg.cs

@@ -4,7 +4,6 @@
 /// 系统角色机构表
 /// </summary>
 [SugarTable("sys_role_org", "系统角色机构表")]
-[SqlSugarEntity]
 public class SysRoleOrg : EntityBaseId
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysTenant.cs

@@ -4,7 +4,6 @@
 /// 系统租户表
 /// </summary>
 [SugarTable("sys_tenant", "系统租户表")]
-[SqlSugarEntity]
 public class SysTenant : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysTimer.cs

@@ -4,7 +4,6 @@
 /// 定时任务
 /// </summary>
 [SugarTable("sys_timer", "定时任务表")]
-[SqlSugarEntity]
 public class SysTimer : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysUser.cs

@@ -4,7 +4,6 @@ namespace Admin.NET.Core;
 /// 系统用户表
 /// </summary>
 [SugarTable("sys_user", "系统用户表")]
-[SqlSugarEntity]
 public class SysUser : EntityTenant
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysUserExtOrgPos.cs

@@ -4,7 +4,6 @@
 /// 系统用户附属机构职位表
 /// </summary>
 [SugarTable("sys_user_ext_org_pos", "系统用户附属机构职位表")]
-[SqlSugarEntity]
 public class SysUserExtOrgPos : EntityBaseId
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysUserOrg.cs

@@ -4,7 +4,6 @@
 /// 系统用户机构表
 /// </summary>
 [SugarTable("sys_user_org", "系统用户机构表")]
-[SqlSugarEntity]
 public class SysUserOrg : EntityBaseId
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/SysUserRole.cs

@@ -4,7 +4,6 @@
 /// 系统用户角色表
 /// </summary>
 [SugarTable("sys_user_role", "系统用户角色表")]
-[SqlSugarEntity]
 public class SysUserRole : EntityBaseId
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/WechatPay.cs

@@ -4,7 +4,6 @@
 /// 微信支付表
 /// </summary>
 [SugarTable("wechat_pay", "微信支付表")]
-[SqlSugarEntity]
 public class WechatPay : EntityBase
 {
     /// <summary>

+ 0 - 1
Admin.NET/Admin.NET.Core/Entity/WechatUser.cs

@@ -4,7 +4,6 @@
 /// 微信用户表
 /// </summary>
 [SugarTable("wechat_user", "微信用户表")]
-[SqlSugarEntity]
 public class WechatUser : EntityBase
 {
     /// <summary>

+ 0 - 10
Admin.NET/Admin.NET.Core/Extension/ObjectExtension.cs

@@ -6,16 +6,6 @@ namespace Admin.NET.Core;
 [SuppressSniffer]
 public static class ObjectExtension
 {
-    /// <summary>
-    /// 获取实体排序
-    /// </summary>
-    /// <param name="type">排序类型</param>
-    /// <returns>int</returns>
-    public static int GetSqlSugarEntityOrder(this Type type)
-    {
-        return !type.IsDefined(typeof(SqlSugarEntityAttribute), true) ? 0 : type.GetCustomAttribute<SqlSugarEntityAttribute>(true).Order;
-    }
-
     /// <summary>
     /// 判断类型是否实现某个泛型
     /// </summary>

+ 1 - 1
Admin.NET/Admin.NET.Core/Extension/RepositoryExtension.cs

@@ -154,7 +154,7 @@ public static class RepositoryExtension
     private static Tuple<string, string> GetTableInfo<T>()
     {
         var entityType = typeof(T);
-        var configId = entityType.GetCustomAttribute<SqlSugarEntityAttribute>().DbConfigId;
+        var configId = entityType.GetCustomAttribute<TenantAttribute>().configId.ToString();
         var tableName = entityType.GetCustomAttribute<SugarTable>().TableName;
         return new Tuple<string, string>(configId, tableName);
     }

+ 2 - 44
Admin.NET/Admin.NET.Core/Option/ConnectionStringsOptions.cs

@@ -5,22 +5,6 @@
 /// </summary>
 public class ConnectionStringsOptions : IConfigurableOptions
 {
-    /// <summary>
-    /// 默认数据库标识
-    /// </summary>
-    public string DefaultConfigId { get; set; } = SqlSugarConst.ConfigId;
-
-    /// <summary>
-    /// 默认数据库类型
-    /// </summary>
-    public string DefaultDbType { get; set; }
-
-    /// <summary>
-    /// 默认数据库连接字符串
-    /// </summary>
-
-    public string DefaultConnection { get; set; }
-
     /// <summary>
     /// 启用初始化库表
     /// </summary>
@@ -32,33 +16,7 @@ public class ConnectionStringsOptions : IConfigurableOptions
     public bool EnableDiffLog { get; set; }
 
     /// <summary>
-    /// 业务库集合
+    /// 数据库配置集合
     /// </summary>
-    public List<DbConfig> DbConfigs { get; set; } = new List<DbConfig>();
-}
-
-/// <summary>
-/// 数据库参数
-/// </summary>
-public class DbConfig
-{
-    /// <summary>
-    /// 数据库编号
-    /// </summary>
-    public string DbConfigId { get; set; }
-
-    /// <summary>
-    /// 数据库类型
-    /// </summary>
-    public string DbType { get; set; }
-
-    /// <summary>
-    /// 数据库连接字符串
-    /// </summary>
-    public string DbConnection { get; set; }
-
-    /// <summary>
-    /// 启用初始化库表
-    /// </summary>
-    public bool EnableInitTable { get; set; }
+    public List<ConnectionConfig> ConnectionConfigs { get; set; }
 }

+ 4 - 26
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarRepository.cs

@@ -6,33 +6,11 @@
 /// <typeparam name="T"></typeparam>
 public class SqlSugarRepository<T> : SimpleClient<T> where T : class, new()
 {
+    // protected ITenant itenant = null; // 多租户事务
+
     public SqlSugarRepository(ISqlSugarClient context = null) : base(context) // 默认值等于null不能少
     {
-        base.Context = App.GetService<ISqlSugarClient>(); // 切换仓储
-
-        // 数据库上下文根据实体切换-业务分库(例如微服务环境)
-        var entityType = typeof(T);
-
-        // 审计日志切换数据库
-        if (entityType == typeof(SysLogAudit) || entityType == typeof(SysLogEx) || entityType == typeof(SysLogOp) || entityType == typeof(SysLogVis) || entityType == typeof(SysConfig))
-        {
-            Context = Context.AsTenant().GetConnectionScope(SqlSugarConst.ConfigId);
-        }
-        else
-        {
-            // 切换框架数据库
-            if (entityType.IsDefined(typeof(SqlSugarEntityAttribute), false))
-            {
-                var tenantAttribute = entityType.GetCustomAttribute<SqlSugarEntityAttribute>()!;
-                Context.AsTenant().ChangeDatabase(tenantAttribute.DbConfigId);
-            }
-
-            // 切换租户数据库
-            if (entityType.IsDefined(typeof(TenantAttribute), false))
-            {
-                var tenantAttribute = entityType.GetCustomAttribute<TenantAttribute>(false)!;
-                Context.AsTenant().ChangeDatabase(tenantAttribute.configId);
-            }
-        }
+        base.Context = App.GetService<ISqlSugarClient>().AsTenant().GetConnectionWithAttr<T>();
+        //itenant = App.GetService<ISqlSugarClient>().AsTenant() ;
     }
 }

+ 18 - 78
Admin.NET/Admin.NET.Core/SqlSugar/SqlSugarSetup.cs

@@ -15,57 +15,26 @@ public static class SqlSugarSetup
         services.AddSingleton<ISqlSugarClient>(provider =>
         {
             var dbOptions = App.GetOptions<ConnectionStringsOptions>();
-            DealConnectionStr(ref dbOptions); // 处理本地库根目录路径
 
-            var connectionConfigs = SqlSugarConst.ConnectionConfigs; // 方便多库生成
             var configureExternalServices = new ConfigureExternalServices
             {
-                EntityService = (type, column) => // 修改列可空
+                EntityService = (type, column) => // 修改列可空-1、带?问号 2、String类型若没有Required
                 {
-                    // 1、带?问号 2、String类型若没有Required
                     if ((type.PropertyType.IsGenericType && type.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                         || (type.PropertyType == typeof(string) && type.GetCustomAttribute<RequiredAttribute>() == null))
                         column.IsNullable = true;
-                },
-                EntityNameService = (type, entity) =>
-                {
-                    //实体类映射库名.表名
-                    var attr = type.GetCustomAttribute<SqlSugarEntityAttribute>();
-                    if (attr == null) return;
-                    var configId = attr.DbConfigId == SqlSugarConst.ConfigId ? SqlSugarConst.CONFIG_DEFAULT_DB : attr.DbConfigId;
-                    var tableName = type.GetCustomAttribute<SugarTable>().TableName;
-                    entity.DbTableName = $"{configId}.{tableName}";
-                    Console.WriteLine("\r\n" + type.Name.PadRight(30, ' ') + "===映射===>     " + entity.DbTableName);
                 }
             };
-            var defaultConnection = new ConnectionConfig()
+            dbOptions.ConnectionConfigs.ForEach(config =>
             {
-                DbType = (DbType)Convert.ToInt32(Enum.Parse(typeof(DbType), dbOptions.DefaultDbType)),
-                ConnectionString = dbOptions.DefaultConnection,
-                IsAutoCloseConnection = true,
-                ConfigId = dbOptions.DefaultConfigId,
-                ConfigureExternalServices = configureExternalServices
-            };
-            connectionConfigs.Add(defaultConnection);
-
-            dbOptions.DbConfigs.ForEach(config =>
-            {
-                var connection = new ConnectionConfig()
-                {
-                    DbType = (DbType)Convert.ToInt32(Enum.Parse(typeof(DbType), config.DbType)),
-                    ConnectionString = config.DbConnection,
-                    IsAutoCloseConnection = true,
-                    ConfigId = config.DbConfigId,
-                    ConfigureExternalServices = configureExternalServices
-                };
-                connectionConfigs.Add(connection);
+                config.ConfigureExternalServices = configureExternalServices;
             });
 
-            SqlSugarScope sqlSugar = new(connectionConfigs, db =>
+            SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs, db =>
             {
-                connectionConfigs.ForEach(config =>
+                dbOptions.ConnectionConfigs.ForEach(config =>
                 {
-                    var dbProvider = db.GetConnection((string)config.ConfigId);
+                    var dbProvider = db.GetConnectionScope((string)config.ConfigId);
 
                     // 设置超时时间
                     dbProvider.Ado.CommandTimeOut = 30;
@@ -80,7 +49,6 @@ public static class SqlSugarSetup
                         if (sql.StartsWith("DELETE"))
                             Console.ForegroundColor = ConsoleColor.Blue;
                         Console.WriteLine("\r\n" + "=========执行SQL============" + "\r\n" + UtilMethods.GetSqlString(DbType.MySql, sql, pars) + "\r\n");
-                        //Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)) + "\r\n" + "========================" + "\r\n");
                         App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
                     };
                     dbProvider.Aop.OnError = (ex) =>
@@ -88,7 +56,6 @@ public static class SqlSugarSetup
                         Console.ForegroundColor = ConsoleColor.Red;
                         var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
                         Console.WriteLine("\r\n" + "=========SQL错误============" + "\r\n" + UtilMethods.GetSqlString(DbType.MySql, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n");
-                        //Console.WriteLine($"{ex.Message}{Environment.NewLine}{ex.Sql}{Environment.NewLine}{pars}{Environment.NewLine}");
                         App.PrintToMiniProfiler("SqlSugar", "Error", $"{ex.Message}{Environment.NewLine}{ex.Sql}{pars}{Environment.NewLine}");
                     };
 
@@ -131,7 +98,6 @@ public static class SqlSugarSetup
                     dbProvider.Aop.OnDiffLogEvent = async u =>
                     {
                         if (!dbOptions.EnableDiffLog) return;
-
                         var LogDiff = new SysLogDiff
                         {
                             // 操作后记录(字段描述、列名、值、表名、表描述)
@@ -175,24 +141,21 @@ public static class SqlSugarSetup
     /// </summary>
     public static void InitDataBase(SqlSugarScope db, ConnectionStringsOptions dbOptions)
     {
-        // 创建默认数据库
-        db.DbMaintenance.CreateDatabase();
-        // 创建业务数据库
-        dbOptions.DbConfigs.ForEach(config =>
+        // 创建数据库
+        dbOptions.ConnectionConfigs.ForEach(config =>
         {
-            db.GetConnection(config.DbConfigId).DbMaintenance.CreateDatabase();
+            db.GetConnectionScope(config.ConfigId).DbMaintenance.CreateDatabase();
         });
 
         // 获取所有实体表
         var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
-            && u.IsDefined(typeof(SqlSugarEntityAttribute), false))
-            .OrderByDescending(u => u.GetSqlSugarEntityOrder());
+            && u.IsDefined(typeof(SugarTable), false));
         if (!entityTypes.Any()) return;
         // 初始化库表结构
         foreach (var entityType in entityTypes)
         {
-            var dbConfigId = entityType.GetCustomAttribute<SqlSugarEntityAttribute>(true).DbConfigId;
-            db.ChangeDatabase(dbConfigId);
+            var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
+            db.ChangeDatabase(tAtt == null ? SqlSugarConst.ConfigId : tAtt.configId);
             db.CodeFirst.InitTables(entityType);
         }
 
@@ -210,8 +173,8 @@ public static class SqlSugarSetup
             if (seedData == null) continue;
 
             var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
-            var dbConfigId = entityType.GetCustomAttribute<SqlSugarEntityAttribute>(true).DbConfigId;
-            db.ChangeDatabase(dbConfigId);
+            var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
+            db.ChangeDatabase(tAtt == null ? SqlSugarConst.ConfigId : tAtt.configId);
 
             var seedDataTable = seedData.ToList().ToDataTable();
             if (seedDataTable.Columns.Contains(SqlSugarConst.PrimaryKey))
@@ -231,7 +194,7 @@ public static class SqlSugarSetup
     /// <summary>
     /// 配置实体假删除过滤器
     /// </summary>
-    public static void SetDeletedEntityFilter(SqlSugarProvider db)
+    public static void SetDeletedEntityFilter(SqlSugarScopeProvider db)
     {
         // 获取所有继承基类数据表集合
         var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
@@ -248,7 +211,7 @@ public static class SqlSugarSetup
     /// <summary>
     /// 配置实体机构过滤器
     /// </summary>
-    public static async void SetOrgEntityFilter(SqlSugarProvider db)
+    public static async void SetOrgEntityFilter(SqlSugarScopeProvider db)
     {
         // 获取业务数据表集合
         var dataEntityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
@@ -272,7 +235,7 @@ public static class SqlSugarSetup
     /// <summary>
     /// 配置自定义实体过滤器
     /// </summary>
-    public static void SetCustomEntityFilter(SqlSugarProvider db)
+    public static void SetCustomEntityFilter(SqlSugarScopeProvider db)
     {
         // 排除超管过滤
         if (App.User?.FindFirst(ClaimConst.SuperAdmin)?.Value == ((int)UserTypeEnum.SuperAdmin).ToString())
@@ -297,7 +260,7 @@ public static class SqlSugarSetup
     /// <summary>
     /// 配置租户实体过滤器
     /// </summary>
-    public static void SetTenantEntityFilter(SqlSugarProvider db)
+    public static void SetTenantEntityFilter(SqlSugarScopeProvider db)
     {
         // 获取租户实体数据表集合
         var dataEntityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
@@ -313,27 +276,4 @@ public static class SqlSugarSetup
             db.QueryFilter.Add(new TableFilterItem<object>(dataEntityType, dynamicExpression));
         }
     }
-
-    /// <summary>
-    /// 处理本地库根目录路径
-    /// </summary>
-    /// <param name="dbOptions"></param>
-    private static void DealConnectionStr(ref ConnectionStringsOptions dbOptions)
-    {
-        if (dbOptions.DefaultDbType.Trim().ToLower() == "sqlite" && dbOptions.DefaultConnection.Contains("./"))
-        {
-            dbOptions.DefaultConnection = UpdateDbPath(dbOptions.DefaultConnection);
-        }
-        dbOptions.DbConfigs.ForEach(cofing =>
-        {
-            if (cofing.DbType.Trim().ToLower() == "sqlite" && cofing.DbConnection.Contains("./"))
-                cofing.DbConnection = UpdateDbPath(cofing.DbConnection);
-        });
-    }
-
-    private static string UpdateDbPath(string dbConnection)
-    {
-        var file = Path.GetFileName(dbConnection.Replace("DataSource=", ""));
-        return $"DataSource={Environment.CurrentDirectory.Replace(@"\bin\Debug", "")}/{file}";
-    }
 }

+ 1 - 2
Admin.NET/Admin.NET.Core/Util/CodeGenUtil.cs

@@ -10,8 +10,7 @@ public static class CodeGenUtil
     // 根据数据库类型来处理对应的数据字段类型
     public static string ConvertDataType(string dataType)
     {
-        var dbTypeStr = App.GetOptions<ConnectionStringsOptions>().DefaultDbType;
-        var dbType = (DbType)Convert.ToInt32(Enum.Parse(typeof(DbType), dbTypeStr));
+        var dbType = App.GetOptions<ConnectionStringsOptions>().ConnectionConfigs[0].DbType;
         return dbType switch
         {
             DbType.PostgreSQL => ConvertDataType_PostgreSQL(dataType),