|
|
@@ -1,5 +1,3 @@
|
|
|
-using Masuit.Tools;
|
|
|
-
|
|
|
namespace Admin.NET.Core;
|
|
|
|
|
|
public static class SqlSugarSetup
|
|
|
@@ -16,21 +14,25 @@ public static class SqlSugarSetup
|
|
|
SetDbConfig(config);
|
|
|
});
|
|
|
|
|
|
- SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs.Adapt<List<ConnectionConfig>>(), client =>
|
|
|
+ SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs.Adapt<List<ConnectionConfig>>(), db =>
|
|
|
{
|
|
|
dbOptions.ConnectionConfigs.ForEach(config =>
|
|
|
{
|
|
|
- var db = client.GetConnectionScope((string)config.ConfigId);
|
|
|
- SetDbAop(db, config);
|
|
|
+ SetDbAop(db.GetConnectionScope(config.ConfigId));
|
|
|
});
|
|
|
});
|
|
|
|
|
|
// 初始化数据库表结构及种子数据
|
|
|
- InitDataBase(sqlSugar, dbOptions);
|
|
|
+ dbOptions.ConnectionConfigs.ForEach(config =>
|
|
|
+ {
|
|
|
+ if (config.EnableInitDb && config.DbType != SqlSugar.DbType.Oracle)
|
|
|
+ sqlSugar.DbMaintenance.CreateDatabase();
|
|
|
+ InitDataBase(sqlSugar.AsTenant(), config);
|
|
|
+ });
|
|
|
|
|
|
services.AddSingleton<ISqlSugarClient>(sqlSugar); // 单例注册
|
|
|
services.AddScoped(typeof(SqlSugarRepository<>)); // 仓储注册
|
|
|
- services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 注册事务与工作单元
|
|
|
+ services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 事务与工作单元注册
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -62,9 +64,10 @@ public static class SqlSugarSetup
|
|
|
/// 配置Aop
|
|
|
/// </summary>
|
|
|
/// <param name="db"></param>
|
|
|
- /// <param name="config"></param>
|
|
|
- public static void SetDbAop(this SqlSugarScopeProvider db, DbConnectionConfig config)
|
|
|
+ public static void SetDbAop(SqlSugarScopeProvider db)
|
|
|
{
|
|
|
+ var config = db.CurrentConnectionConfig;
|
|
|
+
|
|
|
// 设置超时时间
|
|
|
db.Ado.CommandTimeOut = 30;
|
|
|
|
|
|
@@ -165,27 +168,23 @@ public static class SqlSugarSetup
|
|
|
/// <summary>
|
|
|
/// 初始化数据库结构
|
|
|
/// </summary>
|
|
|
- private static void InitDataBase(SqlSugarScope db, DbConnectionOptions dbOptions)
|
|
|
+ /// <param name="db"></param>
|
|
|
+ /// <param name="config"></param>
|
|
|
+ /// <param name="tenantId"></param>
|
|
|
+ public static void InitDataBase(ITenant db, DbConnectionConfig config, long tenantId = 0)
|
|
|
{
|
|
|
- // 创建数据库
|
|
|
- dbOptions.ConnectionConfigs.ForEach(config =>
|
|
|
- {
|
|
|
- if (!config.EnableInitDb || config.DbType == SqlSugar.DbType.Oracle) return;
|
|
|
- db.GetConnectionScope(config.ConfigId).DbMaintenance.CreateDatabase();
|
|
|
- });
|
|
|
-
|
|
|
// 获取所有实体表-初始化表结构
|
|
|
var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
|
|
|
&& u.IsDefined(typeof(SugarTable), false) && !u.IsDefined(typeof(NotTableAttribute), false));
|
|
|
if (!entityTypes.Any()) return;
|
|
|
+ var db2 = db.GetConnectionScope(config.ConfigId);
|
|
|
foreach (var entityType in entityTypes)
|
|
|
{
|
|
|
- var tAtt = entityType.GetCustomAttribute<TenantAttribute>(); // 多数据库
|
|
|
- var configId = tAtt == null ? SqlSugarConst.ConfigId : tAtt.configId.ToString();
|
|
|
- if (!dbOptions.ConnectionConfigs.FirstOrDefault(u => u.ConfigId == configId).EnableInitDb)
|
|
|
- continue;
|
|
|
- var db2 = db.GetConnectionScope(configId);
|
|
|
- var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>(); // 分表
|
|
|
+ var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
|
|
|
+ if (tAtt != null && tAtt.configId.ToString() != config.ConfigId) continue;
|
|
|
+ if (tAtt == null && config.ConfigId != SqlSugarConst.ConfigId && tenantId < 1) continue;
|
|
|
+
|
|
|
+ var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>();
|
|
|
if (splitTable == null)
|
|
|
db2.CodeFirst.InitTables(entityType);
|
|
|
else
|
|
|
@@ -206,19 +205,24 @@ public static class SqlSugarSetup
|
|
|
|
|
|
var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
|
|
|
var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
|
|
|
- var configId = tAtt == null ? SqlSugarConst.ConfigId : tAtt.configId.ToString();
|
|
|
- if (!dbOptions.ConnectionConfigs.FirstOrDefault(u => u.ConfigId == configId).EnableInitDb)
|
|
|
- continue;
|
|
|
- var db2 = db.GetConnectionScope(configId);
|
|
|
+ if (tAtt != null && tAtt.configId.ToString() != config.ConfigId) continue;
|
|
|
+ if (tAtt == null && config.ConfigId != SqlSugarConst.ConfigId && tenantId < 1) continue;
|
|
|
+
|
|
|
var seedDataTable = seedData.ToList().ToDataTable();
|
|
|
- seedDataTable.TableName = db.EntityMaintenance.GetEntityInfo(entityType).DbTableName;
|
|
|
+ seedDataTable.TableName = db2.EntityMaintenance.GetEntityInfo(entityType).DbTableName;
|
|
|
+ // 创建租户库时修改租户Id
|
|
|
+ if (tenantId > 1 && seedDataTable.Columns.Contains(SqlSugarConst.TenantId))
|
|
|
+ {
|
|
|
+ foreach (DataRow dr in seedDataTable.Rows)
|
|
|
+ dr[SqlSugarConst.TenantId] = tenantId;
|
|
|
+ }
|
|
|
if (seedDataTable.Columns.Contains(SqlSugarConst.PrimaryKey))
|
|
|
{
|
|
|
var storage = db2.Storageable(seedDataTable).WhereColumns(SqlSugarConst.PrimaryKey).ToStorage();
|
|
|
storage.AsInsertable.ExecuteCommand();
|
|
|
storage.AsUpdateable.ExecuteCommand();
|
|
|
}
|
|
|
- else // 没有主键或者不是预定义的主键(没主键有重复的可能)
|
|
|
+ else // 没有主键或者不是预定义的主键(有重复的可能)
|
|
|
{
|
|
|
var storage = db2.Storageable(seedDataTable).ToStorage();
|
|
|
storage.AsInsertable.ExecuteCommand();
|
|
|
@@ -227,72 +231,25 @@ public static class SqlSugarSetup
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
- /// 初始化数据库结构
|
|
|
+ /// 增加租户库连接
|
|
|
/// </summary>
|
|
|
- public static void CreateDataBase(ISqlSugarClient db, DbConnectionConfig config, long tenantId)
|
|
|
+ /// <param name="iTenant"></param>
|
|
|
+ /// <param name="tenantId"></param>
|
|
|
+ public static SqlSugarScopeProvider InitTenantDb(ITenant iTenant, long tenantId)
|
|
|
{
|
|
|
- SetDbConfig(config);
|
|
|
-
|
|
|
- var itenant = db.AsTenant();
|
|
|
- // 创建数据库
|
|
|
- if (!config.EnableInitDb || config.DbType == SqlSugar.DbType.Oracle) return;
|
|
|
- itenant.AddConnection(config);
|
|
|
- var dbProvider = itenant.GetConnectionScope(config.ConfigId);
|
|
|
- SetDbAop(dbProvider, config);
|
|
|
- dbProvider.DbMaintenance.CreateDatabase();
|
|
|
-
|
|
|
- // 获取所有实体表-初始化表结构
|
|
|
- var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
|
|
|
- && u.IsDefined(typeof(SugarTable), false) && !u.IsDefined(typeof(NotTableAttribute), false));
|
|
|
- if (!entityTypes.Any()) return;
|
|
|
- foreach (var entityType in entityTypes)
|
|
|
- {
|
|
|
- var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
|
|
|
- if (tAtt != null) continue;
|
|
|
- var db2 = itenant.GetConnectionScope(config.ConfigId);
|
|
|
- var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>();
|
|
|
- if (splitTable == null)
|
|
|
- db2.CodeFirst.InitTables(entityType);
|
|
|
- else
|
|
|
- db2.CodeFirst.SplitTables().InitTables(entityType);
|
|
|
- }
|
|
|
-
|
|
|
- // 获取所有种子配置-初始化数据
|
|
|
- var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
|
|
|
- && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))));
|
|
|
- if (!seedDataTypes.Any()) return;
|
|
|
- foreach (var seedType in seedDataTypes)
|
|
|
+ var tenant = App.GetRequiredService<SysCacheService>().Get<List<SysTenant>>(CacheConst.KeyTenant).FirstOrDefault(u => u.Id == tenantId);
|
|
|
+ if (!iTenant.IsAnyConnection(tenantId.ToString()))
|
|
|
{
|
|
|
- var instance = Activator.CreateInstance(seedType);
|
|
|
-
|
|
|
- var hasDataMethod = seedType.GetMethod("HasData");
|
|
|
- var seedData = ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>();
|
|
|
- if (seedData == null) continue;
|
|
|
-
|
|
|
- var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
|
|
|
- var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
|
|
|
- if (tAtt != null) continue;
|
|
|
- var db2 = itenant.GetConnectionScope(config.ConfigId);
|
|
|
- var seedDataTable = seedData.ToList().ToDataTable();
|
|
|
- seedDataTable.TableName = db.EntityMaintenance.GetEntityInfo(entityType).DbTableName;
|
|
|
- // 设置租户Id
|
|
|
- if (seedDataTable.Columns.Contains(SqlSugarConst.TenantId))
|
|
|
- {
|
|
|
- foreach (DataRow dr in seedDataTable.Rows)
|
|
|
- dr[SqlSugarConst.TenantId] = tenantId;
|
|
|
- }
|
|
|
- if (seedDataTable.Columns.Contains(SqlSugarConst.PrimaryKey))
|
|
|
+ iTenant.AddConnection(new ConnectionConfig()
|
|
|
{
|
|
|
- var storage = db2.Storageable(seedDataTable).WhereColumns(SqlSugarConst.PrimaryKey).ToStorage();
|
|
|
- storage.AsInsertable.ExecuteCommand();
|
|
|
- storage.AsUpdateable.ExecuteCommand();
|
|
|
- }
|
|
|
- else // 没有主键或者不是预定义的主键(没主键有重复的可能)
|
|
|
- {
|
|
|
- var storage = db2.Storageable(seedDataTable).ToStorage();
|
|
|
- storage.AsInsertable.ExecuteCommand();
|
|
|
- }
|
|
|
+ ConfigId = tenantId.ToString(),
|
|
|
+ ConnectionString = tenant.Connection,
|
|
|
+ DbType = tenant.DbType,
|
|
|
+ IsAutoCloseConnection = true
|
|
|
+ });
|
|
|
+ SetDbAop(iTenant.GetConnectionScope(tenantId.ToString()));
|
|
|
}
|
|
|
+ return iTenant.GetConnectionScope(tenantId.ToString());
|
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
|
@@ -452,7 +409,7 @@ public static class SqlSugarSetup
|
|
|
var entityFilters = ((IList)entityFilterMethod?.Invoke(instance, null))?.Cast<object>();
|
|
|
if (entityFilters == null) continue;
|
|
|
|
|
|
- entityFilters.ForEach(u =>
|
|
|
+ foreach (var u in entityFilters)
|
|
|
{
|
|
|
var tableFilterItem = (TableFilterItem<object>)u;
|
|
|
var entityType = tableFilterItem.GetType().GetProperty("type", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(tableFilterItem, null) as Type;
|
|
|
@@ -464,7 +421,7 @@ public static class SqlSugarSetup
|
|
|
|
|
|
tableFilterItems.Add(tableFilterItem);
|
|
|
db.QueryFilter.Add(tableFilterItem);
|
|
|
- });
|
|
|
+ }
|
|
|
}
|
|
|
db.DataCache.Add(cacheKey, tableFilterItems);
|
|
|
}
|