diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextFactory.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextFactory.cs index ffcf1224..ad3bddd0 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextFactory.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/SqlSugarDbContextFactory.cs @@ -22,8 +22,10 @@ namespace Yi.Framework.SqlSugarCore private IAbpLazyServiceProvider LazyServiceProvider { get; } + private TenantConfigurationWrapper TenantConfigurationWrapper=> LazyServiceProvider.LazyGetRequiredService(); private ICurrentTenant CurrentTenant => LazyServiceProvider.LazyGetRequiredService(); - public DbConnOptions Options => LazyServiceProvider.LazyGetRequiredService>().Value; + + private DbConnOptions Options => LazyServiceProvider.LazyGetRequiredService>().Value; private ISerializeService SerializeService => LazyServiceProvider.LazyGetRequiredService(); @@ -36,19 +38,13 @@ namespace Yi.Framework.SqlSugarCore { LazyServiceProvider = lazyServiceProvider; - var connectionString = GetCurrentConnectionString(); - + var tenantConfiguration= AsyncHelper.RunSync(async () =>await TenantConfigurationWrapper.GetAsync()); + var connectionConfig =BuildConnectionConfig(action: options => { - options.ConnectionString = connectionString; - options.DbType = GetCurrentDbType(); + options.ConnectionString =tenantConfiguration.GetCurrentConnectionString(); + options.DbType = GetCurrentDbType(tenantConfiguration.GetCurrentConnectionName()); }); - // var connectionConfig = ConnectionConfigCache.GetOrAdd(connectionString, (_) => - // BuildConnectionConfig(action: options => - // { - // options.ConnectionString = connectionString; - // options.DbType = GetCurrentDbType(); - // })); SqlSugarClient = new SqlSugarClient(connectionConfig); //生命周期,以下都可以直接使用sqlsugardb了 @@ -188,38 +184,15 @@ namespace Yi.Framework.SqlSugarCore return connectionConfig; } - - /// - /// db切换多库支持 - /// - /// - protected virtual string GetCurrentConnectionString() + + protected virtual DbType GetCurrentDbType(string tenantName) { - var connectionStringResolver = LazyServiceProvider.LazyGetRequiredService(); - var connectionString = - AsyncHelper.RunSync(() => connectionStringResolver.ResolveAsync()); - - if (string.IsNullOrWhiteSpace(connectionString)) + if (tenantName == ConnectionStrings.DefaultConnectionStringName) { - Check.NotNull(Options.Url, "dbUrl未配置"); + return Options.DbType!.Value; } - - 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; - } - } - - Check.NotNull(Options.DbType, "默认DbType未配置!"); - return Options.DbType!.Value; + var dbTypeFromTenantName = GetDbTypeFromTenantName(tenantName); + return dbTypeFromTenantName!.Value; } //根据租户name进行匹配db类型: Test_Sqlite,[来自AI] diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/TenantConfigurationWrapper.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/TenantConfigurationWrapper.cs new file mode 100644 index 00000000..bcfb61cf --- /dev/null +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/TenantConfigurationWrapper.cs @@ -0,0 +1,79 @@ +using Volo.Abp.Data; +using Volo.Abp.DependencyInjection; +using Volo.Abp.MultiTenancy; + +namespace Yi.Framework.SqlSugarCore; + +/// +/// 租户配置 +/// +public class TenantConfigurationWrapper : ITransientDependency +{ + private ICurrentTenant _currentTenant; + private ITenantStore _tenantStore; + + public TenantConfigurationWrapper(ICurrentTenant currentTenant, ITenantStore tenantStore) + { + _currentTenant = currentTenant; + _tenantStore = tenantStore; + } + + /// + /// 获取租户信息 + /// + /// + public async Task GetAsync() + { + if (_currentTenant.Id is not null) + { + return await _tenantStore.FindAsync(_currentTenant.Id.Value); + } + else if (!string.IsNullOrEmpty(_currentTenant.Name)) + { + return await _tenantStore.FindAsync(_currentTenant.Name); + } + else + { + return await _tenantStore.FindAsync(ConnectionStrings.DefaultConnectionStringName); + } + } + + /// + /// 获取当前连接字符串 + /// + /// + public async Task GetCurrentConnectionStringAsync() + { + return (await GetAsync()).ConnectionStrings.Default!; + } + /// + /// 获取当前连接名 + /// + /// + public async Task GetCurrentConnectionNameAsync() + { + return (await GetAsync()).Name; + } +} + +public static class TenantConfigurationExtensions +{ + /// + /// 获取当前连接字符串 + /// + /// + public static string GetCurrentConnectionString(this TenantConfiguration tenantConfiguration) + { + return tenantConfiguration.ConnectionStrings.Default!; + } + + /// + /// 获取当前连接名 + /// + /// + public static string GetCurrentConnectionName(this TenantConfiguration tenantConfiguration) + { + return tenantConfiguration.Name; + } +} + 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 34a48521..c235ffd8 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/Uow/UnitOfWorkSqlsugarDbContextProvider.cs @@ -15,8 +15,8 @@ namespace Yi.Framework.SqlSugarCore.Uow { public ILogger> Logger { get; set; } public IServiceProvider ServiceProvider { get; set; } - private static AsyncLocalDbContextAccessor ContextInstance => AsyncLocalDbContextAccessor.Instance; + protected readonly TenantConfigurationWrapper _tenantConfigurationWrapper; protected readonly IUnitOfWorkManager UnitOfWorkManager; protected readonly IConnectionStringResolver ConnectionStringResolver; protected readonly ICancellationTokenProvider CancellationTokenProvider; @@ -26,26 +26,25 @@ namespace Yi.Framework.SqlSugarCore.Uow IUnitOfWorkManager unitOfWorkManager, IConnectionStringResolver connectionStringResolver, ICancellationTokenProvider cancellationTokenProvider, - ICurrentTenant currentTenant - ) + ICurrentTenant currentTenant, TenantConfigurationWrapper tenantConfigurationWrapper) { UnitOfWorkManager = unitOfWorkManager; ConnectionStringResolver = connectionStringResolver; CancellationTokenProvider = cancellationTokenProvider; CurrentTenant = currentTenant; + _tenantConfigurationWrapper = tenantConfigurationWrapper; Logger = NullLogger>.Instance; } public virtual async Task GetDbContextAsync() { - - var connectionStringName = ConnectionStrings.DefaultConnectionStringName; - //获取当前连接字符串,未多租户时,默认为空 - var connectionString = await ResolveConnectionStringAsync(connectionStringName); + var tenantConfiguration= await _tenantConfigurationWrapper.GetAsync(); + //由于sqlsugar的特殊性,没有db区分,不再使用连接字符串解析器 + var connectionStringName = tenantConfiguration.GetCurrentConnectionName(); + var connectionString = tenantConfiguration.GetCurrentConnectionString(); var dbContextKey = $"{this.GetType().Name}_{connectionString}"; - - + var unitOfWork = UnitOfWorkManager.Current; if (unitOfWork == null ) { @@ -67,8 +66,6 @@ namespace Yi.Framework.SqlSugarCore.Uow databaseApi = new SqlSugarDatabaseApi( await CreateDbContextAsync(unitOfWork, connectionStringName, connectionString) ); - - //await Console.Out.WriteLineAsync(">>>----------------实例化了db"+ ((SqlSugarDatabaseApi)databaseApi).DbContext.SqlSugarClient.ContextID.ToString()); //创建的db加入到当前工作单元中 unitOfWork.AddDatabaseApi(dbContextKey, databaseApi); @@ -98,7 +95,7 @@ namespace Yi.Framework.SqlSugarCore.Uow protected virtual async Task CreateDbContextWithTransactionAsync(IUnitOfWork unitOfWork) { //事务key - var transactionApiKey = $"SqlsugarCore_{SqlSugarDbContextCreationContext.Current.ConnectionString}"; + var transactionApiKey = $"SqlSugarCore_{SqlSugarDbContextCreationContext.Current.ConnectionString}"; //尝试查找事务 var activeTransaction = unitOfWork.FindTransactionApi(transactionApiKey) as SqlSugarTransactionApi; @@ -123,20 +120,5 @@ namespace Yi.Framework.SqlSugarCore.Uow } - - - protected virtual async Task ResolveConnectionStringAsync(string connectionStringName) - { - if (typeof(TDbContext).IsDefined(typeof(IgnoreMultiTenancyAttribute), false)) - { - using (CurrentTenant.Change(null)) - { - return await ConnectionStringResolver.ResolveAsync(connectionStringName); - } - } - - return await ConnectionStringResolver.ResolveAsync(connectionStringName); - } - } } diff --git a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs index 4d83a783..c5175be7 100644 --- a/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs +++ b/Yi.Abp.Net8/framework/Yi.Framework.SqlSugarCore/YiFrameworkSqlSugarCoreModule.cs @@ -10,6 +10,8 @@ using Volo.Abp.Data; using Volo.Abp.Domain; using Volo.Abp.Domain.Repositories; using Volo.Abp.Guids; +using Volo.Abp.MultiTenancy; +using Volo.Abp.MultiTenancy.ConfigurationStore; using Yi.Framework.SqlSugarCore.Abstractions; using Yi.Framework.SqlSugarCore.Repositories; using Yi.Framework.SqlSugarCore.Uow; @@ -69,7 +71,15 @@ namespace Yi.Framework.SqlSugarCore var dbConfig = section.Get(); //将默认db传递给abp连接字符串模块 Configure(x => { x.ConnectionStrings.Default = dbConfig.Url; }); - + //配置abp默认租户 + Configure(x => { x.Tenants.AddFirst(new TenantConfiguration + { + Id = Guid.Empty, + Name =$"{ConnectionStrings.DefaultConnectionStringName}", + NormalizedName = ConnectionStrings.DefaultConnectionStringName, + ConnectionStrings = new ConnectionStrings(){{ConnectionStrings.DefaultConnectionStringName,dbConfig.Url}}, + IsActive = true + });}); context.Services.AddYiDbContext(); return Task.CompletedTask; } diff --git a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiMultiTenantConnectionStringResolver.cs b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiMultiTenantConnectionStringResolver.cs index 6da0fd34..8bfd07ff 100644 --- a/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiMultiTenantConnectionStringResolver.cs +++ b/Yi.Abp.Net8/module/tenant-management/Yi.Framework.TenantManagement.Domain/YiMultiTenantConnectionStringResolver.cs @@ -53,10 +53,8 @@ public class YiMultiTenantConnectionStringResolver : DefaultConnectionStringReso : Options.ConnectionStrings.Default!; } - - //Requesting specific connection string... - var connString = tenant.ConnectionStrings?.FirstOrDefault().Value; + var connString = tenant.ConnectionStrings?.GetOrDefault(connectionStringName); if (!connString.IsNullOrWhiteSpace()) { //Found for the tenant