feat: 完成db创建优化
This commit is contained in:
@@ -63,8 +63,8 @@ namespace Yi.Framework.SqlSugarCore.Abstractions
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public List<SaasMultiTenancyOptions>? SaasMultiTenancy { get; set; }
|
public List<SaasMultiTenancyOptions>? SaasMultiTenancy { get; set; }
|
||||||
|
|
||||||
public static string MasterTenantDbDefaultName = "Master";
|
public static string MasterTenantName = "Master";
|
||||||
public static string TenantDbDefaultName = "Default";
|
public static string DefaultTenantName = "Default";
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 获取默认数据库
|
/// 获取默认数据库
|
||||||
@@ -72,7 +72,7 @@ namespace Yi.Framework.SqlSugarCore.Abstractions
|
|||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
public SaasMultiTenancyOptions GetDefaultSaasMultiTenancy()
|
public SaasMultiTenancyOptions GetDefaultSaasMultiTenancy()
|
||||||
{
|
{
|
||||||
return new SaasMultiTenancyOptions { Name = TenantDbDefaultName, Url = Url };
|
return new SaasMultiTenancyOptions { Name = DefaultTenantName, Url = Url };
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -88,13 +88,13 @@ namespace Yi.Framework.SqlSugarCore.Abstractions
|
|||||||
if (string.IsNullOrEmpty(MasterSaasMultiTenancyUrl))
|
if (string.IsNullOrEmpty(MasterSaasMultiTenancyUrl))
|
||||||
{
|
{
|
||||||
|
|
||||||
return new SaasMultiTenancyOptions { Name = MasterTenantDbDefaultName, Url = Url };
|
return new SaasMultiTenancyOptions { Name = MasterTenantName, Url = Url };
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
return new SaasMultiTenancyOptions()
|
return new SaasMultiTenancyOptions()
|
||||||
{
|
{
|
||||||
Name = MasterTenantDbDefaultName,
|
Name = MasterTenantName,
|
||||||
Url = MasterSaasMultiTenancyUrl
|
Url = MasterSaasMultiTenancyUrl
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Security.Policy;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
@@ -24,7 +25,7 @@ namespace Yi.Framework.SqlSugarCore
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public ISqlSugarClient SqlSugarClient { get; private set; }
|
public ISqlSugarClient SqlSugarClient { get; private set; }
|
||||||
public ICurrentUser CurrentUser => LazyServiceProvider.GetRequiredService<ICurrentUser>();
|
public ICurrentUser CurrentUser => LazyServiceProvider.GetRequiredService<ICurrentUser>();
|
||||||
private readonly string MasterTenantDbDefaultName = DbConnOptions.MasterTenantDbDefaultName;
|
private readonly string MasterTenantDbDefaultName = DbConnOptions.MasterTenantName;
|
||||||
private IAbpLazyServiceProvider LazyServiceProvider { get; }
|
private IAbpLazyServiceProvider LazyServiceProvider { get; }
|
||||||
|
|
||||||
private IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetRequiredService<IGuidGenerator>();
|
private IGuidGenerator GuidGenerator => LazyServiceProvider.LazyGetRequiredService<IGuidGenerator>();
|
||||||
@@ -54,67 +55,36 @@ namespace Yi.Framework.SqlSugarCore
|
|||||||
connectionCreator.DataExecuted = DataExecuted;
|
connectionCreator.DataExecuted = DataExecuted;
|
||||||
connectionCreator.OnLogExecuting = OnLogExecuting;
|
connectionCreator.OnLogExecuting = OnLogExecuting;
|
||||||
connectionCreator.OnLogExecuted = OnLogExecuted;
|
connectionCreator.OnLogExecuted = OnLogExecuted;
|
||||||
SqlSugarClient = new SqlSugarClient(connectionCreator.Build());
|
var currentConnection = GetCurrentConnectionString();
|
||||||
var connectionStringResolver = LazyServiceProvider.LazyGetRequiredService<IConnectionStringResolver>();
|
SqlSugarClient = new SqlSugarClient(connectionCreator.Build(action: options =>
|
||||||
var connectionStr = connectionStringResolver.ResolveAsync().Result;
|
{
|
||||||
var changedDb = DatabaseChange(this, connectionStr);
|
options.ConnectionString = currentConnection;
|
||||||
SqlSugarClient = changedDb.SqlSugarClient;
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// db切换多库支持
|
/// db切换多库支持
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="dbContext"></param>
|
|
||||||
/// <param name="connectionString"></param>
|
|
||||||
/// <returns></returns>
|
/// <returns></returns>
|
||||||
protected virtual SqlSugarDbContext DatabaseChange(SqlSugarDbContext dbContext, string connectionString)
|
protected virtual string GetCurrentConnectionString()
|
||||||
{
|
{
|
||||||
string configId = string.Empty;
|
var connectionStringResolver = LazyServiceProvider.LazyGetRequiredService<IConnectionStringResolver>();
|
||||||
|
var connectionString = connectionStringResolver.ResolveAsync().Result;
|
||||||
|
|
||||||
//没有检测到使用多租户功能,默认使用默认库即可
|
//没有检测到使用多租户功能,默认使用默认库即可
|
||||||
if (string.IsNullOrWhiteSpace(connectionString))
|
if (string.IsNullOrWhiteSpace(connectionString))
|
||||||
{
|
{
|
||||||
connectionString = dbContext.Options.Url;
|
Volo.Abp.Check.NotNull(Options.Url, "租户默认库Defalut未找到");
|
||||||
configId = CurrentTenant.Name;
|
connectionString = Options.Url;
|
||||||
}
|
}
|
||||||
|
//如果当前租户是主库,单独使用主要库
|
||||||
var dbOption = dbContext.Options;
|
|
||||||
var db = dbContext.SqlSugarClient.AsTenant();
|
|
||||||
|
|
||||||
//主库的Db切换,当操作的是租户表的时候
|
|
||||||
if (CurrentTenant.Name == MasterTenantDbDefaultName)
|
if (CurrentTenant.Name == MasterTenantDbDefaultName)
|
||||||
{
|
{
|
||||||
//直接切换
|
var conStrOrNull = Options.GetMasterSaasMultiTenancy();
|
||||||
configId = MasterTenantDbDefaultName;
|
Volo.Abp.Check.NotNull(conStrOrNull, "租户主库Master未找到");
|
||||||
var conStrOrNull = dbOption.GetMasterSaasMultiTenancy();
|
|
||||||
Volo.Abp.Check.NotNull(conStrOrNull, "租户主库未找到");
|
|
||||||
connectionString = conStrOrNull.Url;
|
connectionString = conStrOrNull.Url;
|
||||||
}
|
}
|
||||||
|
return connectionString!;
|
||||||
//租户Db的动态切换
|
|
||||||
//二级缓存
|
|
||||||
var changed = false;
|
|
||||||
if (!db.IsAnyConnection(configId))
|
|
||||||
{
|
|
||||||
var config = _dbConnectionCreator.Build(options =>
|
|
||||||
{
|
|
||||||
options.DbType = dbOption.DbType!.Value;
|
|
||||||
options.ConfigId = configId;//设置库的唯一标识
|
|
||||||
options.IsAutoCloseConnection = true;
|
|
||||||
options.ConnectionString = connectionString;
|
|
||||||
});
|
|
||||||
//添加一个db到当前上下文 (Add部分不线上下文不会共享)
|
|
||||||
db.AddConnection(config);
|
|
||||||
changed = true;
|
|
||||||
}
|
|
||||||
var currentDb = db.GetConnection(configId) as ISqlSugarClient;
|
|
||||||
//设置Aop
|
|
||||||
if (changed)
|
|
||||||
{
|
|
||||||
_dbConnectionCreator.SetDbAop(currentDb);
|
|
||||||
}
|
|
||||||
dbContext.SetSqlSugarClient(currentDb);
|
|
||||||
|
|
||||||
return dbContext;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -20,10 +20,15 @@ namespace Yi.Framework.Rbac.Domain.Shared.Consts
|
|||||||
public const string No_Permission = "登录禁用!该用户分配无任何权限,无意义登录!";
|
public const string No_Permission = "登录禁用!该用户分配无任何权限,无意义登录!";
|
||||||
public const string No_Role = "登录禁用!该用户分配无任何角色,无意义登录!";
|
public const string No_Role = "登录禁用!该用户分配无任何角色,无意义登录!";
|
||||||
|
|
||||||
|
//子租户管理员
|
||||||
public const string Admin = "cc";
|
public const string Admin = "cc";
|
||||||
public const string AdminRolesCode = "admin";
|
public const string AdminRolesCode = "admin";
|
||||||
public const string AdminPermissionCode = "*:*:*";
|
public const string AdminPermissionCode = "*:*:*";
|
||||||
|
|
||||||
|
//租户管理员
|
||||||
|
public const string TenantAdmin = "ccadmin";
|
||||||
|
public const string TenantAdminPermissionCode = "*";
|
||||||
|
|
||||||
public const string DefaultRoleCode = "default";
|
public const string DefaultRoleCode = "default";
|
||||||
public const string CommonRoleName = "common";
|
public const string CommonRoleName = "common";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -110,7 +110,7 @@ namespace Yi.Framework.TenantManagement.Application
|
|||||||
[HttpPut("tenant/init/{id}")]
|
[HttpPut("tenant/init/{id}")]
|
||||||
public async Task InitAsync([FromRoute]Guid id)
|
public async Task InitAsync([FromRoute]Guid id)
|
||||||
{
|
{
|
||||||
using (CurrentTenant.Change(id,"test"))
|
using (CurrentTenant.Change(id))
|
||||||
{
|
{
|
||||||
await CodeFirst(this.LazyServiceProvider);
|
await CodeFirst(this.LazyServiceProvider);
|
||||||
await _dataSeeder.SeedAsync(id);
|
await _dataSeeder.SeedAsync(id);
|
||||||
|
|||||||
@@ -1,9 +1,4 @@
|
|||||||
using System;
|
using Volo.Abp.MultiTenancy;
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text;
|
|
||||||
using System.Threading.Tasks;
|
|
||||||
using Volo.Abp.MultiTenancy;
|
|
||||||
using Yi.Framework.SqlSugarCore.Abstractions;
|
using Yi.Framework.SqlSugarCore.Abstractions;
|
||||||
|
|
||||||
namespace Yi.Framework.TenantManagement.Domain
|
namespace Yi.Framework.TenantManagement.Domain
|
||||||
@@ -12,7 +7,12 @@ namespace Yi.Framework.TenantManagement.Domain
|
|||||||
{
|
{
|
||||||
public static IDisposable ChangeMaster(this ICurrentTenant currentTenant)
|
public static IDisposable ChangeMaster(this ICurrentTenant currentTenant)
|
||||||
{
|
{
|
||||||
return currentTenant.Change(null, DbConnOptions.MasterTenantDbDefaultName);
|
return currentTenant.Change(null, DbConnOptions.MasterTenantName);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IDisposable ChangeDefalut(this ICurrentTenant currentTenant)
|
||||||
|
{
|
||||||
|
return currentTenant.Change(null, DbConnOptions.DefaultTenantName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
using Microsoft.Extensions.DependencyInjection.Extensions;
|
||||||
|
using Volo.Abp.Data;
|
||||||
using Volo.Abp.Domain;
|
using Volo.Abp.Domain;
|
||||||
using Volo.Abp.Modularity;
|
using Volo.Abp.Modularity;
|
||||||
using Volo.Abp.MultiTenancy;
|
using Volo.Abp.MultiTenancy;
|
||||||
@@ -15,7 +16,9 @@ namespace Yi.Framework.TenantManagement.Domain
|
|||||||
public override void ConfigureServices(ServiceConfigurationContext context)
|
public override void ConfigureServices(ServiceConfigurationContext context)
|
||||||
{
|
{
|
||||||
var services = context.Services;
|
var services = context.Services;
|
||||||
services.Replace(new ServiceDescriptor(typeof(ITenantStore),typeof(SqlSugarTenantStore), ServiceLifetime.Transient));
|
services.Replace(new ServiceDescriptor(typeof(ITenantStore), typeof(SqlSugarTenantStore), ServiceLifetime.Transient));
|
||||||
|
|
||||||
|
services.Replace(new ServiceDescriptor(typeof(IConnectionStringResolver), typeof(YiMultiTenantConnectionStringResolver), ServiceLifetime.Transient));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,12 @@ using Volo.Abp.DependencyInjection;
|
|||||||
namespace Volo.Abp.MultiTenancy;
|
namespace Volo.Abp.MultiTenancy;
|
||||||
|
|
||||||
[Dependency(ReplaceServices = true)]
|
[Dependency(ReplaceServices = true)]
|
||||||
public class MultiTenantConnectionStringResolver2 : DefaultConnectionStringResolver
|
public class YiMultiTenantConnectionStringResolver : DefaultConnectionStringResolver
|
||||||
{
|
{
|
||||||
private readonly ICurrentTenant _currentTenant;
|
private readonly ICurrentTenant _currentTenant;
|
||||||
private readonly IServiceProvider _serviceProvider;
|
private readonly IServiceProvider _serviceProvider;
|
||||||
|
|
||||||
public MultiTenantConnectionStringResolver2(
|
public YiMultiTenantConnectionStringResolver(
|
||||||
IOptionsMonitor<AbpDbConnectionOptions> options,
|
IOptionsMonitor<AbpDbConnectionOptions> options,
|
||||||
ICurrentTenant currentTenant,
|
ICurrentTenant currentTenant,
|
||||||
IServiceProvider serviceProvider)
|
IServiceProvider serviceProvider)
|
||||||
@@ -1,8 +1,5 @@
|
|||||||
using Microsoft.Extensions.DependencyInjection.Extensions;
|
|
||||||
using Serilog;
|
using Serilog;
|
||||||
using Serilog.Events;
|
using Serilog.Events;
|
||||||
using Volo.Abp.Data;
|
|
||||||
using Volo.Abp.MultiTenancy;
|
|
||||||
using Yi.Abp.Web;
|
using Yi.Abp.Web;
|
||||||
|
|
||||||
//创建日志,可使用{SourceContext}记录
|
//创建日志,可使用{SourceContext}记录
|
||||||
@@ -26,7 +23,6 @@ try
|
|||||||
builder.Host.UseAutofac();
|
builder.Host.UseAutofac();
|
||||||
builder.Host.UseSerilog();
|
builder.Host.UseSerilog();
|
||||||
await builder.Services.AddApplicationAsync<YiAbpWebModule>();
|
await builder.Services.AddApplicationAsync<YiAbpWebModule>();
|
||||||
builder.Services.Replace(new ServiceDescriptor(typeof(IConnectionStringResolver), typeof(MultiTenantConnectionStringResolver2),ServiceLifetime.Transient));
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
await app.InitializeApplicationAsync();
|
await app.InitializeApplicationAsync();
|
||||||
await app.RunAsync();
|
await app.RunAsync();
|
||||||
|
|||||||
Binary file not shown.
Binary file not shown.
Reference in New Issue
Block a user