diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs index 76a032e3..46427f65 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContext.cs @@ -1,4 +1,5 @@ -using System.Reflection; +using System; +using System.Reflection; using System.Security.Policy; using System.Text; using Microsoft.Extensions.DependencyInjection; @@ -37,7 +38,7 @@ namespace Yi.Framework.SqlSugarCore public IEntityChangeEventHelper EntityChangeEventHelper => LazyServiceProvider.LazyGetService(NullEntityChangeEventHelper.Instance); public DbConnOptions Options => LazyServiceProvider.LazyGetRequiredService>().Value; - public AbpDbConnectionOptions ConnectionOptions=> LazyServiceProvider.LazyGetRequiredService>().Value; + public AbpDbConnectionOptions ConnectionOptions => LazyServiceProvider.LazyGetRequiredService>().Value; private ISqlSugarDbConnectionCreator _dbConnectionCreator; public void SetSqlSugarClient(ISqlSugarClient sqlSugarClient) @@ -56,9 +57,11 @@ namespace Yi.Framework.SqlSugarCore connectionCreator.OnLogExecuting = OnLogExecuting; connectionCreator.OnLogExecuted = OnLogExecuted; var currentConnection = GetCurrentConnectionString(); + var currentDbType = GetCurrentDbType(); SqlSugarClient = new SqlSugarClient(connectionCreator.Build(action: options => { options.ConnectionString = currentConnection; + })); connectionCreator.SetDbAop(SqlSugarClient); } @@ -78,7 +81,7 @@ namespace Yi.Framework.SqlSugarCore //开启了多租户 var connectionStringResolver = LazyServiceProvider.LazyGetRequiredService(); - var connectionString = connectionStringResolver.ResolveAsync().Result; + var connectionString = connectionStringResolver.ResolveAsync().GetAwaiter().GetResult(); //没有检测到使用多租户功能,默认使用默认库即可 @@ -90,6 +93,50 @@ namespace Yi.Framework.SqlSugarCore return connectionString!; } + protected virtual DbType GetCurrentDbType() + { + if (CurrentTenant.Name is not null) + { + var dbTypeFromTenantName = GetDbTypeFromTenantName(CurrentTenant.Name); + if (dbTypeFromTenantName is not null) + { + return dbTypeFromTenantName.Value; + } + } + Volo.Abp.Check.NotNull(Options.DbType, "默认DbType未配置!"); + return Options.DbType!.Value; + } + + //根据租户name进行匹配db类型: Test_Sqlite,[来自AI] + private DbType? GetDbTypeFromTenantName(string name) + { + if (string.IsNullOrWhiteSpace(name)) + { + return null; + } + + // 查找下划线的位置 + int underscoreIndex = name.LastIndexOf('_'); + + if (underscoreIndex == -1 || underscoreIndex == name.Length - 1) + { + return null; + } + + // 提取 枚举 部分 + string enumString = name.Substring(underscoreIndex + 1); + + // 尝试将 尾缀 转换为枚举 + if (Enum.TryParse(enumString, out DbType result)) + { + return result; + } + + // 条件不满足时返回 null + return null; + } + + /// /// 上下文对象扩展 diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs index f96e7e81..09a660bc 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs @@ -14,7 +14,7 @@ namespace Yi.Framework.SqlSugarCore.Uow public class UnitOfWorkSqlsugarDbContextProvider : ISugarDbContextProvider where TDbContext : ISqlSugarDbContext { private readonly ISqlSugarDbConnectionCreator _dbConnectionCreator; - + public ILogger> Logger { get; set; } public IServiceProvider ServiceProvider { get; set; } @@ -64,8 +64,7 @@ namespace Yi.Framework.SqlSugarCore.Uow - //lock (_databaseApiLock) - //{ + //尝试当前工作单元获取db var databaseApi = unitOfWork.FindDatabaseApi(dbContextKey); @@ -74,7 +73,7 @@ namespace Yi.Framework.SqlSugarCore.Uow { //db根据连接字符串来创建 databaseApi = new SqlSugarDatabaseApi( - CreateDbContextAsync(unitOfWork, connectionStringName, connectionString).Result + await CreateDbContextAsync(unitOfWork, connectionStringName, connectionString) ); //await Console.Out.WriteLineAsync(">>>----------------实例化了db"+ ((SqlSugarDatabaseApi)databaseApi).DbContext.SqlSugarClient.ContextID.ToString()); @@ -83,8 +82,6 @@ namespace Yi.Framework.SqlSugarCore.Uow } return (TDbContext)((SqlSugarDatabaseApi)databaseApi).DbContext; - //} - } diff --git a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/SqlSugarTenantStore.cs b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/SqlSugarAndConfigurationTenantStore.cs similarity index 69% rename from Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/SqlSugarTenantStore.cs rename to Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/SqlSugarAndConfigurationTenantStore.cs index 34750ce5..f5bfaa52 100644 --- a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/SqlSugarTenantStore.cs +++ b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/SqlSugarAndConfigurationTenantStore.cs @@ -1,42 +1,64 @@ -using JetBrains.Annotations; +using System.Xml.Linq; +using JetBrains.Annotations; +using Microsoft.Extensions.Options; using Volo.Abp; using Volo.Abp.Caching; using Volo.Abp.Data; using Volo.Abp.MultiTenancy; +using Volo.Abp.MultiTenancy.ConfigurationStore; namespace Yi.Framework.TenantManagement.Domain { - public class SqlSugarTenantStore : ITenantStore + public class SqlSugarAndConfigurationTenantStore : DefaultTenantStore, ITenantStore { private ISqlSugarTenantRepository TenantRepository { get; } protected ICurrentTenant CurrentTenant { get; } protected IDistributedCache Cache { get; } - public SqlSugarTenantStore(ISqlSugarTenantRepository repository, + public SqlSugarAndConfigurationTenantStore(ISqlSugarTenantRepository repository, IDistributedCache cache, - ICurrentTenant currentTenant) - { TenantRepository = repository; - Cache=cache; - CurrentTenant=currentTenant; + ICurrentTenant currentTenant, + IOptionsMonitor options) : base(options) + { + TenantRepository = repository; + Cache = cache; + CurrentTenant = currentTenant; } - public TenantConfiguration? Find(string name) + public new TenantConfiguration? Find(string name) { throw new NotImplementedException("请使用异步方法"); } - public TenantConfiguration? Find(Guid id) + public new TenantConfiguration? Find(Guid id) { throw new NotImplementedException("请使用异步方法"); } - public async Task FindAsync(string name) + public new async Task FindAsync(string name) { - return (await GetCacheItemAsync(null, name)).Value; + var tenantFromOptions = await base.FindAsync(name); + //如果配置文件不存在改租户 + if (tenantFromOptions is null) + { + return (await GetCacheItemAsync(null, name)).Value; + } + else + { + return tenantFromOptions; + } } - public async Task FindAsync(Guid id) + public new async Task FindAsync(Guid id) { - return (await GetCacheItemAsync(id, null)).Value; + var tenantFromOptions = await base.FindAsync(id); + if (tenantFromOptions is null) + { + return (await GetCacheItemAsync(id, null)).Value; + } + else + { + return tenantFromOptions; + } } diff --git a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiFrameworkTenantManagementDomainModule.cs b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiFrameworkTenantManagementDomainModule.cs index 87301d07..3a359b3e 100644 --- a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiFrameworkTenantManagementDomainModule.cs +++ b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiFrameworkTenantManagementDomainModule.cs @@ -16,7 +16,7 @@ namespace Yi.Framework.TenantManagement.Domain public override void ConfigureServices(ServiceConfigurationContext context) { var services = context.Services; - services.Replace(new ServiceDescriptor(typeof(ITenantStore), typeof(SqlSugarTenantStore), ServiceLifetime.Transient)); + services.Replace(new ServiceDescriptor(typeof(ITenantStore), typeof(SqlSugarAndConfigurationTenantStore), ServiceLifetime.Transient)); services.Replace(new ServiceDescriptor(typeof(IConnectionStringResolver), typeof(YiMultiTenantConnectionStringResolver), ServiceLifetime.Transient)); } diff --git a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiTenantConfigurationProvider.cs b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiTenantConfigurationProvider.cs index 0914e47c..bb5852d4 100644 --- a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiTenantConfigurationProvider.cs +++ b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiTenantConfigurationProvider.cs @@ -26,6 +26,7 @@ public class YiTenantConfigurationProvider : ITenantConfigurationProvider, ITran public virtual async Task GetAsync(bool saveResolveResult = false) { + //租户解析器获取到当前解析成功的租户 var resolveResult = await TenantResolver.ResolveTenantIdOrNameAsync(); if (saveResolveResult) @@ -36,6 +37,7 @@ public class YiTenantConfigurationProvider : ITenantConfigurationProvider, ITran TenantConfiguration? tenant = null; if (resolveResult.TenantIdOrName != null) { + //根据租户信息获取租户 tenant = await FindTenantAsync(resolveResult.TenantIdOrName); if (tenant == null) diff --git a/Yi.Abp.Net8/src/Yi.Abp.Web/yi-abp-dev.db b/Yi.Abp.Net8/src/Yi.Abp.Web/yi-abp-dev.db deleted file mode 100644 index 6c2ef117..00000000 Binary files a/Yi.Abp.Net8/src/Yi.Abp.Web/yi-abp-dev.db and /dev/null differ