控制台显示正在初始化的表和种子数据

支持拆分表数据初始化
菜单icon增加默认值
This commit is contained in:
coolcalf 2024-07-21 16:32:44 +08:00
parent 357cfb57d7
commit 8361c21532
3 changed files with 32 additions and 13 deletions

View File

@ -4,7 +4,7 @@
// SqlSugar PostgreSQL
// https://www.connectionstrings.com/
"DbConnection": {
"EnableConsoleSql": true, // SQL
"EnableConsoleSql": false, // SQL
"ConnectionConfigs": [
{
//"ConfigId": "1300000000001", // -

View File

@ -74,7 +74,7 @@ public partial class SysMenu : EntityBase
/// </summary>
[SugarColumn(ColumnDescription = "图标", Length = 128)]
[MaxLength(128)]
public string? Icon { get; set; }
public string? Icon { get; set; } = "ele-Setting";
/// <summary>
/// 是否内嵌

View File

@ -284,6 +284,7 @@ public static class SqlSugarSetup
// 初始化/创建数据库
if (config.DbSettings.EnableInitDb)
{
Log.Information($"初始化 {config.DbType} 数据库 {config.ConfigId} {config.ConnectionString}");
if (config.DbType != SqlSugar.DbType.Oracle)
dbProvider.DbMaintenance.CreateDatabase();
}
@ -291,6 +292,7 @@ public static class SqlSugarSetup
// 初始化表结构
if (config.TableSettings.EnableInitTable)
{
Log.Information($"初始化 {config.DbType} 表结构 {config.ConfigId}");
var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false))
.Where(u => !u.GetCustomAttributes<IgnoreTableAttribute>().Any())
.WhereIF(config.TableSettings.EnableIncreTable, u => u.IsDefined(typeof(IncreTableAttribute), false)).ToList();
@ -301,9 +303,11 @@ public static class SqlSugarSetup
entityTypes = entityTypes.Where(u => u.GetCustomAttributes<LogTableAttribute>().Any()).ToList();
else
entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库
int count = 0, sum = entityTypes.Count();
foreach (var entityType in entityTypes)
{
Console.WriteLine($"创建表({config.ConfigId} - {++count}/{sum}){entityType}");
if (entityType.GetCustomAttribute<SplitTableAttribute>() == null)
dbProvider.CodeFirst.InitTables(entityType);
else
@ -314,10 +318,12 @@ public static class SqlSugarSetup
// 初始化种子数据
if (config.SeedSettings.EnableInitSeed)
{
Log.Information($"初始化 {config.DbType} 种子数据 {config.ConfigId}");
var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
.WhereIF(config.SeedSettings.EnableIncreSeed, u => u.IsDefined(typeof(IncreSeedAttribute), false))
.OrderBy(u => u.GetCustomAttributes(typeof(SeedDataAttribute), false).Length > 0 ? ((SeedDataAttribute)u.GetCustomAttributes(typeof(SeedDataAttribute), false)[0]).Order : 0).ToList();
int count = 0, sum = seedDataTypes.Count();
foreach (var seedType in seedDataTypes)
{
var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
@ -343,19 +349,32 @@ public static class SqlSugarSetup
if (seedData == null) continue;
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
if (entityInfo.Columns.Any(u => u.IsPrimarykey))
Console.WriteLine($"添加数据({config.ConfigId} - {++count}/{sum}){entityInfo.DbTableName}");
if (entityType.GetCustomAttribute<SplitTableAttribute>(true) != null)
{
// 按主键进行批量增加和更新
var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage();
storage.AsInsertable.ExecuteCommand();
if (seedType.GetCustomAttribute<IgnoreUpdateSeedAttribute>() == null) // 有忽略更新种子特性时则不更新
storage.AsUpdateable.IgnoreColumns(entityInfo.Columns.Where(u => u.PropertyInfo.GetCustomAttribute<IgnoreUpdateSeedColumnAttribute>() != null).Select(u => u.PropertyName).ToArray()).ExecuteCommand();
//拆分表的操作需要实体类型,而通过反射很难实现
//所以这里将Init方法写在“种子数据类”内部再传入 db 反射调用
var hasInitMethod = seedType.GetMethod("Init");
var parameters = new object[] { db };
hasInitMethod?.Invoke(instance, parameters);
}
else
{
// 无主键则只进行插入
if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any())
dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand();
if (entityInfo.Columns.Any(u => u.IsPrimarykey))
{
// 按主键进行批量增加和更新
var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage();
storage.AsInsertable.ExecuteCommand();
if (seedType.GetCustomAttribute<IgnoreUpdateSeedAttribute>() == null) // 有忽略更新种子特性时则不更新
storage.AsUpdateable.IgnoreColumns(entityInfo.Columns.Where(u => u.PropertyInfo.GetCustomAttribute<IgnoreUpdateSeedColumnAttribute>() != null).Select(u => u.PropertyName).ToArray()).ExecuteCommand();
}
else
{
// 无主键则只进行插入
if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any())
dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand();
}
}
}
}