💡refactor(表实体初始化): 重构表结构初始化逻辑
This commit is contained in:
parent
6aa54325fd
commit
4835af760b
@ -444,4 +444,39 @@ public static class RepositoryExtension
|
|||||||
throw Oops.Oh(error);
|
throw Oops.Oh(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化表实体
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="dbProvider"></param>
|
||||||
|
/// <typeparam name="T"></typeparam>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static void InitTable<T>(this SqlSugarScopeProvider dbProvider) where T : class, new()
|
||||||
|
{
|
||||||
|
InitTable(dbProvider, typeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 初始化表实体
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="entityType"></param>
|
||||||
|
/// <param name="dbProvider"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static void InitTable(this SqlSugarScopeProvider dbProvider, Type entityType)
|
||||||
|
{
|
||||||
|
// 初始化表实体,如果存在分表特性,需要额外处理
|
||||||
|
if (entityType.GetCustomAttribute<SplitTableAttribute>() == null)
|
||||||
|
dbProvider.CodeFirst.InitTables(entityType);
|
||||||
|
else
|
||||||
|
dbProvider.CodeFirst.SplitTables().InitTables(entityType);
|
||||||
|
|
||||||
|
// 将不存在实体中的字段改为可空
|
||||||
|
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
|
||||||
|
var dbColumnInfos = dbProvider.DbMaintenance.GetColumnInfosByTableName(entityInfo.DbTableName) ?? [];
|
||||||
|
foreach (var dbColumnInfo in dbColumnInfos.Where(dbColumnInfo => !dbColumnInfo.IsPrimarykey && entityInfo.Columns.All(u => u.DbColumnName != dbColumnInfo.DbColumnName)))
|
||||||
|
{
|
||||||
|
dbColumnInfo.IsNullable = true;
|
||||||
|
dbProvider.DbMaintenance.UpdateColumn(entityInfo.DbTableName, dbColumnInfo);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -391,26 +391,26 @@ public static class SqlSugarSetup
|
|||||||
else
|
else
|
||||||
entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库
|
entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库
|
||||||
|
|
||||||
|
var semaphore = new SemaphoreSlim(8); // 并发限制数量
|
||||||
int taskIndex = 0, entityTypeCount = entityTypes.Count;
|
int taskIndex = 0, entityTypeCount = entityTypes.Count;
|
||||||
var taskList = entityTypes.Select(entityType => Task.Run(() =>
|
var taskList = entityTypes.Select(entityType => Task.Run(() =>
|
||||||
{
|
{
|
||||||
DateTime st = DateTime.Now;
|
semaphore.Wait(); // 获取信号量许可
|
||||||
if (entityType.GetCustomAttribute<SplitTableAttribute>() == null)
|
try
|
||||||
dbProvider.CodeFirst.InitTables(entityType);
|
|
||||||
else
|
|
||||||
dbProvider.CodeFirst.SplitTables().InitTables(entityType);
|
|
||||||
// 将不存在实体中的字段改为可空
|
|
||||||
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
|
|
||||||
var dbColumnInfos = dbProvider.DbMaintenance.GetColumnInfosByTableName(entityInfo.DbTableName) ?? [];
|
|
||||||
var tempDbColumnInfo = dbColumnInfos.Where(dbColumnInfo => !dbColumnInfo.IsPrimarykey && entityInfo.Columns.All(u => u.DbColumnName != null && u.DbColumnName?.ToLower() != dbColumnInfo.DbColumnName?.ToLower()));
|
|
||||||
foreach (var dbColumnInfo in tempDbColumnInfo)
|
|
||||||
{
|
{
|
||||||
dbColumnInfo.IsNullable = true;
|
var stopWatch = Stopwatch.StartNew(); // 开始计时
|
||||||
dbProvider.DbMaintenance.UpdateColumn(entityInfo.DbTableName, dbColumnInfo);
|
|
||||||
}
|
dbProvider.InitTable(entityType); // 同步表结构
|
||||||
DateTime et = DateTime.Now;
|
|
||||||
|
stopWatch.Stop(); // 停止计时
|
||||||
|
|
||||||
Console.ForegroundColor = ConsoleColor.Green;
|
Console.ForegroundColor = ConsoleColor.Green;
|
||||||
Console.WriteLine($"初始化表 {entityType,-64} ({config.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{entityTypeCount:D003}) 用时:{et.Subtract(st).TotalMilliseconds}ms");
|
Console.WriteLine($"初始化表 {entityType,-64} ({config.ConfigId} - {Interlocked.Increment(ref taskIndex):D003}/{entityTypeCount:D003}) 用时:{stopWatch.ElapsedMilliseconds}ms");
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
semaphore.Release(); // 释放信号量许可
|
||||||
|
}
|
||||||
}));
|
}));
|
||||||
Task.WaitAll(taskList.ToArray());
|
Task.WaitAll(taskList.ToArray());
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user