Browse Source

数据库配置新增指定程序集dbconfig.providerType

zhontai 2 years ago
parent
commit
02f7497ed4

+ 3 - 0
src/hosts/ZhonTai.Host/Configs/dbconfig.json

@@ -37,6 +37,9 @@
   //PostgreSQL "Host=localhost;Port=5432;Username=postgres;Password=; Database=admindb;Pooling=true;Minimum Pool Size=1"
   //Sqlite "Data Source=|DataDirectory|\\admindb.db; Pooling=true;Min Pool Size=1"
   "connectionString": "Data Source=|DataDirectory|\\admindb.db; Pooling=true;Min Pool Size=1",
+  //指定程序集
+  //FreeSql.MySql.MySqlProvider`1,FreeSql.Provider.MySqlConnector
+  "providerType": "",
 
   //空闲时间(分)
   "idleTime": 10,

+ 1 - 1
src/platform/ZhonTai.Admin/Core/Attributes/TransactionAttribute.cs

@@ -24,6 +24,6 @@ public class TransactionAttribute : Attribute
     /// <summary>
     /// 数据库键
     /// </summary>
-    public string DbKey { get; set; } = DbKeys.MasterDbKey;
+    public string DbKey { get; set; } = DbKeys.AdminDbKey;
 
 }

+ 5 - 0
src/platform/ZhonTai.Admin/Core/Configs/DbConfig.cs

@@ -38,6 +38,11 @@ public class DbConfig
     /// </summary>
     public string ConnectionString { get; set; } = "Data Source=|DataDirectory|\\admindb.db; Pooling=true;Min Pool Size=1";
 
+    /// <summary>
+    /// 指定程序集
+    /// </summary>
+    public string ProviderType { get; set; }
+
     /// <summary>
     /// 空闲时间(分)
     /// </summary>

+ 3 - 3
src/platform/ZhonTai.Admin/Core/Consts/DbKeys.cs

@@ -8,10 +8,10 @@ namespace ZhonTai.Admin.Core.Consts;
 public static partial class DbKeys
 {
     /// <summary>
-    /// 
+    /// admin
     /// </summary>
-    [Description("库")]
-    public const string MasterDbKey = "masterdb";
+    [Description("admin库")]
+    public const string AdminDbKey = "admindb";
 
     /// <summary>
     /// 多库

+ 3 - 3
src/platform/ZhonTai.Admin/Core/Db/DBServiceCollectionExtensions.cs

@@ -24,9 +24,9 @@ public static class DBServiceCollectionExtensions
     /// <param name="env"></param>
     /// <param name="hostAppOptions"></param>
     /// <returns></returns>
-    public static void AddMasterDb(this IServiceCollection services, FreeSqlCloud freeSqlCloud, IHostEnvironment env, HostAppOptions hostAppOptions)
+    public static void AddAdminDb(this IServiceCollection services, FreeSqlCloud freeSqlCloud, IHostEnvironment env, HostAppOptions hostAppOptions)
     {
-        freeSqlCloud.Register(DbKeys.MasterDbKey, () =>
+        freeSqlCloud.Register(DbKeys.AdminDbKey, () =>
         {
             var dbConfig = ConfigHelper.Get<DbConfig>("dbconfig", env.EnvironmentName);
 
@@ -39,7 +39,7 @@ public static class DBServiceCollectionExtensions
             #region FreeSql
 
             var freeSqlBuilder = new FreeSqlBuilder()
-                    .UseConnectionString(dbConfig.Type, dbConfig.ConnectionString)
+                    .UseConnectionString(dbConfig.Type, dbConfig.ConnectionString, dbConfig.ProviderType.NotNull() ? Type.GetType(dbConfig.ProviderType) : null)
                     .UseAutoSyncStructure(false)
                     .UseLazyLoading(false)
                     .UseNoneCommandParameter(true);

+ 4 - 4
src/platform/ZhonTai.Admin/Core/Db/FreeSqlCloudExtesions.cs

@@ -108,8 +108,8 @@ public static class FreeSqlCloudExtesions
             if (!exists)
             {
                 var dbConfig = serviceProvider.GetRequiredService<DbConfig>();
-                var masterDb = cloud.Use(DbKeys.MasterDbKey);
-                var tenant = masterDb.Select<TenantEntity>().DisableGlobalFilter("Tenant").WhereDynamic(tenantId).ToOne<CreateFreeSqlTenantDto>();
+                var uowm = serviceProvider.GetRequiredService<UnitOfWorkManagerCloud>();
+                var tenant = uowm.GetUnitOfWorkManager(DbKeys.AdminDbKey).Orm.GetRepositoryBase<TenantEntity>().Select.DisableGlobalFilter("Tenant").WhereDynamic(tenantId).ToOne<CreateFreeSqlTenantDto>();
                 cloud.Register(tenantName, () => CreateFreeSql(user, appConfig, dbConfig, tenant));
             }
 
@@ -117,7 +117,7 @@ public static class FreeSqlCloudExtesions
         }
         else
         {
-            var masterDb = cloud.Use(DbKeys.MasterDbKey);
+            var masterDb = cloud.Use(DbKeys.AdminDbKey);
             return masterDb;
         }
     }
@@ -144,7 +144,7 @@ public static class FreeSqlCloudExtesions
         {
             var dbConfig = serviceProvider.GetRequiredService<DbConfig>();
             var uowm = serviceProvider.GetRequiredService<UnitOfWorkManagerCloud>();
-            var tenant = uowm.GetUnitOfWorkManager(DbKeys.MasterDbKey).Orm.Select<TenantEntity>().DisableGlobalFilter("Tenant").WhereDynamic(tenantId).ToOne<CreateFreeSqlTenantDto>();
+            var tenant = uowm.GetUnitOfWorkManager(DbKeys.AdminDbKey).Orm.GetRepositoryBase<TenantEntity>().Select.DisableGlobalFilter("Tenant").WhereDynamic(tenantId).ToOne<CreateFreeSqlTenantDto>();
             //var timeSpan = tenant.IdleTime.HasValue && tenant.IdleTime.Value > 0 ? TimeSpan.FromMinutes(tenant.IdleTime.Value) : TimeSpan.MaxValue;
             cloud.Register(tenantName, () => CreateFreeSql(user, appConfig, dbConfig, tenant));
         }

+ 3 - 2
src/platform/ZhonTai.Admin/Core/HostApp.cs

@@ -174,12 +174,13 @@ public class HostApp
         var dbConfig = ConfigHelper.Get<DbConfig>("dbconfig", env.EnvironmentName);
         services.AddSingleton(dbConfig);
 
-        //添加数据库
+        //添加admin数据库
         var freeSqlCloud = new FreeSqlCloud(dbConfig.DistributeKey);
         services.AddSingleton<IFreeSql>(freeSqlCloud);
         services.AddSingleton(freeSqlCloud);
         services.AddScoped<UnitOfWorkManagerCloud>();
-        services.AddMasterDb(freeSqlCloud, env, _hostAppOptions);
+        services.AddAdminDb(freeSqlCloud, env, _hostAppOptions);
+        services.AddSingleton(provider => freeSqlCloud.Use(DbKeys.AdminDbKey));
 
 
         //上传配置

+ 1 - 1
src/platform/ZhonTai.Admin/Core/Repositories/RepositoryBase.cs

@@ -118,7 +118,7 @@ namespace ZhonTai.Admin.Core.Repositories
 
     public class RepositoryBase<TEntity> : RepositoryBase<TEntity, long>, IRepositoryBase<TEntity> where TEntity : class
     {
-        public RepositoryBase(UnitOfWorkManagerCloud uowm) : this(DbKeys.MasterDbKey, uowm) { }
+        public RepositoryBase(UnitOfWorkManagerCloud uowm) : this(DbKeys.AdminDbKey, uowm) { }
         public RepositoryBase(string db, UnitOfWorkManagerCloud uowm) : this(uowm.GetUnitOfWorkManager(db)) { }
         RepositoryBase(UnitOfWorkManager uowm) : base(uowm.Orm)
         {

+ 1 - 1
src/platform/ZhonTai.Admin/Services/Permission/PermissionService.cs

@@ -445,7 +445,7 @@ public class PermissionService : BaseService, IPermissionService, IDynamicApi
         if (_appConfig.Tenant && User.TenantType == TenantType.Tenant)
         {
             var cloud = ServiceProvider.GetRequiredService<FreeSqlCloud>();
-            var tenantPermissionIds = await cloud.Use(DbKeys.MasterDbKey).Select<TenantPermissionEntity>().Where(d => d.TenantId == User.TenantId).ToListAsync(m => m.PermissionId);
+            var tenantPermissionIds = await cloud.Use(DbKeys.AdminDbKey).Select<TenantPermissionEntity>().Where(d => d.TenantId == User.TenantId).ToListAsync(m => m.PermissionId);
             insertPermissionIds = insertPermissionIds.Where(d => tenantPermissionIds.Contains(d));
         }
 

+ 3 - 7
src/platform/ZhonTai.Admin/Services/Tenant/TenantService.cs

@@ -19,10 +19,6 @@ using ZhonTai.Admin.Core.Configs;
 using ZhonTai.Admin.Domain.Org;
 using ZhonTai.Admin.Domain.Staff;
 using ZhonTai.Admin.Domain;
-using FreeSql;
-using Microsoft.Extensions.DependencyInjection;
-using ZhonTai.Admin.Core.Db;
-using ZhonTai.Admin.Core.Db.Transaction;
 
 namespace ZhonTai.Admin.Services.Tenant;
 
@@ -118,9 +114,9 @@ public class TenantService : BaseService, ITenantService, IDynamicApi
         TenantEntity tenant = await _tenantRepository.InsertAsync(entity);
         long tenantId = tenant.Id;
 
-        var freeSqlCloud = LazyGetRequiredService<FreeSqlCloud>();
-        freeSqlCloud.GetTenantDb(ServiceProvider, tenantId);
-        freeSqlCloud.Change(DbKeys.TenantDbKey + tenantId);
+        //var freeSqlCloud = LazyGetRequiredService<FreeSqlCloud>();
+        //freeSqlCloud.GetTenantDb(ServiceProvider, tenantId);
+        //freeSqlCloud.Change(DbKeys.TenantDbKey + tenantId);
 
         //添加部门
         var org = new OrgEntity

+ 1 - 0
src/platform/ZhonTai.Admin/ZhonTai.Admin.csproj

@@ -42,6 +42,7 @@
 		<PackageReference Include="FreeSql.Provider.PostgreSQL" Version="3.2.665" />
 		<PackageReference Include="FreeSql.Provider.Oracle" Version="3.2.665" />
 		<PackageReference Include="FreeSql.Provider.Sqlite" Version="3.2.665" />
+		<PackageReference Include="FreeSql.Provider.MySqlConnector" Version="3.2.680" />
 	</ItemGroup>
 
 	<ItemGroup>

+ 8 - 3
src/platform/ZhonTai.Admin/ZhonTai.Admin.xml

@@ -430,6 +430,11 @@
             数据库字符串
             </summary>
         </member>
+        <member name="P:ZhonTai.Admin.Core.Configs.DbConfig.ProviderType">
+            <summary>
+            指定供应商类型
+            </summary>
+        </member>
         <member name="P:ZhonTai.Admin.Core.Configs.DbConfig.IdleTime">
             <summary>
             空闲时间(分)
@@ -645,9 +650,9 @@
             数据库键名
             </summary>
         </member>
-        <member name="F:ZhonTai.Admin.Core.Consts.DbKeys.MasterDbKey">
+        <member name="F:ZhonTai.Admin.Core.Consts.DbKeys.AdminDbKey">
             <summary>
-            
+            admin
             </summary>
         </member>
         <member name="F:ZhonTai.Admin.Core.Consts.DbKeys.MultiDbKey">
@@ -757,7 +762,7 @@
             <returns></returns>
             <exception cref="T:System.Exception"></exception>
         </member>
-        <member name="M:ZhonTai.Admin.Core.Db.DBServiceCollectionExtensions.AddMasterDb(Microsoft.Extensions.DependencyInjection.IServiceCollection,FreeSql.FreeSqlCloud,Microsoft.Extensions.Hosting.IHostEnvironment,ZhonTai.Admin.Core.Startup.HostAppOptions)">
+        <member name="M:ZhonTai.Admin.Core.Db.DBServiceCollectionExtensions.AddAdminDb(Microsoft.Extensions.DependencyInjection.IServiceCollection,FreeSql.FreeSqlCloud,Microsoft.Extensions.Hosting.IHostEnvironment,ZhonTai.Admin.Core.Startup.HostAppOptions)">
             <summary>
             添加主数据库
             </summary>