2024-11-27 09:53:20 +08:00
|
|
|
|
// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
|
2024-07-04 17:15:52 +08:00
|
|
|
|
//
|
|
|
|
|
|
// 本项目主要遵循 MIT 许可证和 Apache 许可证(版本 2.0)进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
|
|
|
|
|
|
//
|
|
|
|
|
|
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动!任何基于本项目二次开发而产生的一切法律纠纷和责任,我们不承担任何责任!
|
|
|
|
|
|
|
|
|
|
|
|
namespace Admin.NET.Core;
|
|
|
|
|
|
|
|
|
|
|
|
public static class SqlSugarSetup
|
|
|
|
|
|
{
|
|
|
|
|
|
// 多租户实例
|
|
|
|
|
|
public static ITenant ITenant { get; set; }
|
|
|
|
|
|
|
2024-08-08 15:04:39 +08:00
|
|
|
|
// 是否正在处理种子数据
|
|
|
|
|
|
private static bool _isHandlingSeedData = false;
|
|
|
|
|
|
|
2024-07-04 17:15:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// SqlSugar 上下文初始化
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="services"></param>
|
|
|
|
|
|
public static void AddSqlSugar(this IServiceCollection services)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 注册雪花Id
|
|
|
|
|
|
var snowIdOpt = App.GetConfig<SnowIdOptions>("SnowId", true);
|
2024-12-25 10:16:27 +08:00
|
|
|
|
services.AddYitIdHelper(snowIdOpt);
|
2024-07-04 17:15:52 +08:00
|
|
|
|
|
2024-12-25 14:57:25 +08:00
|
|
|
|
// 自定义雪花Id算法
|
|
|
|
|
|
StaticConfig.CustomSnowFlakeFunc = YitIdHelper.NextId;
|
2024-11-25 01:37:03 +08:00
|
|
|
|
// 配置字符串表达式
|
2024-11-23 11:42:42 +08:00
|
|
|
|
StaticConfig.DynamicExpressionParserType = typeof(DynamicExpressionParser);
|
|
|
|
|
|
StaticConfig.DynamicExpressionParsingConfig = new ParsingConfig
|
|
|
|
|
|
{
|
|
|
|
|
|
CustomTypeProvider = new SqlSugarTypeProvider()
|
|
|
|
|
|
};
|
2024-07-04 17:15:52 +08:00
|
|
|
|
|
2024-11-25 01:37:03 +08:00
|
|
|
|
// 初始化所有库连接
|
2024-07-04 17:15:52 +08:00
|
|
|
|
var dbOptions = App.GetConfig<DbConnectionOptions>("DbConnection", true);
|
|
|
|
|
|
dbOptions.ConnectionConfigs.ForEach(SetDbConfig);
|
|
|
|
|
|
|
|
|
|
|
|
SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs.Adapt<List<ConnectionConfig>>(), db =>
|
|
|
|
|
|
{
|
|
|
|
|
|
dbOptions.ConnectionConfigs.ForEach(config =>
|
|
|
|
|
|
{
|
|
|
|
|
|
var dbProvider = db.GetConnectionScope(config.ConfigId);
|
|
|
|
|
|
SetDbAop(dbProvider, dbOptions.EnableConsoleSql);
|
|
|
|
|
|
SetDbDiffLog(dbProvider, config);
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
ITenant = sqlSugar;
|
|
|
|
|
|
|
|
|
|
|
|
services.AddSingleton<ISqlSugarClient>(sqlSugar); // 单例注册
|
|
|
|
|
|
services.AddScoped(typeof(SqlSugarRepository<>)); // 仓储注册
|
|
|
|
|
|
services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 事务与工作单元注册
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化数据库表结构及种子数据
|
|
|
|
|
|
dbOptions.ConnectionConfigs.ForEach(config =>
|
|
|
|
|
|
{
|
|
|
|
|
|
InitDatabase(sqlSugar, config);
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 配置连接属性
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="config"></param>
|
|
|
|
|
|
public static void SetDbConfig(DbConnectionConfig config)
|
|
|
|
|
|
{
|
2024-11-21 11:51:51 +08:00
|
|
|
|
// 解密数据库连接串
|
|
|
|
|
|
if (config.DbSettings.EnableConnEncrypt)
|
|
|
|
|
|
config.ConnectionString = CryptogramUtil.Decrypt(config.ConnectionString);
|
|
|
|
|
|
|
2024-12-17 17:04:27 +08:00
|
|
|
|
// SqlFunc 扩展函数
|
|
|
|
|
|
var sqlFuncServices = new List<SqlFuncExternal>
|
|
|
|
|
|
{
|
|
|
|
|
|
// 密码解密
|
|
|
|
|
|
new SqlFuncExternal()
|
|
|
|
|
|
{
|
|
|
|
|
|
UniqueMethodName = "SqlFuncDecrypt",
|
|
|
|
|
|
MethodValue = (expInfo, dbType, expContext) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
return CryptogramUtil.Decrypt(expInfo.Args[0].MemberName.ToString());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-07-04 17:15:52 +08:00
|
|
|
|
var configureExternalServices = new ConfigureExternalServices
|
|
|
|
|
|
{
|
|
|
|
|
|
EntityNameService = (type, entity) => // 处理表
|
|
|
|
|
|
{
|
|
|
|
|
|
entity.IsDisabledDelete = true; // 禁止删除非 sqlsugar 创建的列
|
|
|
|
|
|
// 只处理贴了特性[SugarTable]表
|
|
|
|
|
|
if (!type.GetCustomAttributes<SugarTable>().Any())
|
|
|
|
|
|
return;
|
|
|
|
|
|
if (config.DbSettings.EnableUnderLine && !entity.DbTableName.Contains('_'))
|
|
|
|
|
|
entity.DbTableName = UtilMethods.ToUnderLine(entity.DbTableName); // 驼峰转下划线
|
|
|
|
|
|
},
|
|
|
|
|
|
EntityService = (type, column) => // 处理列
|
|
|
|
|
|
{
|
|
|
|
|
|
// 只处理贴了特性[SugarColumn]列
|
|
|
|
|
|
if (!type.GetCustomAttributes<SugarColumn>().Any())
|
|
|
|
|
|
return;
|
|
|
|
|
|
if (new NullabilityInfoContext().Create(type).WriteState is NullabilityState.Nullable)
|
|
|
|
|
|
column.IsNullable = true;
|
|
|
|
|
|
if (config.DbSettings.EnableUnderLine && !column.IsIgnore && !column.DbColumnName.Contains('_'))
|
|
|
|
|
|
column.DbColumnName = UtilMethods.ToUnderLine(column.DbColumnName); // 驼峰转下划线
|
|
|
|
|
|
},
|
|
|
|
|
|
DataInfoCacheService = new SqlSugarCache(),
|
2024-12-17 17:04:27 +08:00
|
|
|
|
SqlFuncServices = sqlFuncServices
|
2024-07-04 17:15:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
config.ConfigureExternalServices = configureExternalServices;
|
|
|
|
|
|
config.InitKeyType = InitKeyType.Attribute;
|
|
|
|
|
|
config.IsAutoCloseConnection = true;
|
|
|
|
|
|
config.MoreSettings = new ConnMoreSettings
|
|
|
|
|
|
{
|
|
|
|
|
|
IsAutoRemoveDataCache = true, // 启用自动删除缓存,所有增删改会自动调用.RemoveDataCache()
|
|
|
|
|
|
IsAutoDeleteQueryFilter = true, // 启用删除查询过滤器
|
|
|
|
|
|
IsAutoUpdateQueryFilter = true, // 启用更新查询过滤器
|
|
|
|
|
|
SqlServerCodeFirstNvarchar = true // 采用Nvarchar
|
|
|
|
|
|
};
|
2024-07-24 10:50:40 +08:00
|
|
|
|
|
|
|
|
|
|
// 若库类型是人大金仓则默认设置PG模式
|
|
|
|
|
|
if (config.DbType == SqlSugar.DbType.Kdbndp)
|
|
|
|
|
|
config.MoreSettings.DatabaseModel = SqlSugar.DbType.PostgreSQL; // 配置PG模式主要是兼容系统表差异
|
2024-10-13 21:59:23 +08:00
|
|
|
|
|
|
|
|
|
|
// 若库类型是Oracle则默认主键名字和参数名字最大长度
|
|
|
|
|
|
if (config.DbType == SqlSugar.DbType.Oracle)
|
|
|
|
|
|
config.MoreSettings.MaxParameterNameLength = 30;
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-12-17 17:04:27 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// SqlFunc 扩展函数定义(密码解密)
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
|
|
|
/// <param name="password"></param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
/// <exception cref="NotSupportedException"></exception>
|
|
|
|
|
|
public static string SqlFuncDecrypt<T>(T password)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotSupportedException("Can only be used in expressions");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-04 17:15:52 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 配置Aop
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="db"></param>
|
|
|
|
|
|
/// <param name="enableConsoleSql"></param>
|
|
|
|
|
|
public static void SetDbAop(SqlSugarScopeProvider db, bool enableConsoleSql)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 设置超时时间
|
|
|
|
|
|
db.Ado.CommandTimeOut = 30;
|
|
|
|
|
|
|
|
|
|
|
|
// 打印SQL语句
|
2024-11-27 09:53:20 +08:00
|
|
|
|
db.Aop.OnError = ex =>
|
|
|
|
|
|
{
|
|
|
|
|
|
if (ex.Parametres == null) return;
|
|
|
|
|
|
var log = $"【{DateTime.Now}——错误SQL】\r\n{UtilMethods.GetNativeSql(ex.Sql, (SugarParameter[])ex.Parametres)}\r\n";
|
|
|
|
|
|
Log.Error(log, ex);
|
|
|
|
|
|
App.PrintToMiniProfiler("SqlSugar", "Error", log);
|
|
|
|
|
|
};
|
2024-07-04 17:15:52 +08:00
|
|
|
|
if (enableConsoleSql)
|
|
|
|
|
|
{
|
|
|
|
|
|
db.Aop.OnLogExecuting = (sql, pars) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
//// 若参数值超过100个字符则进行截取
|
|
|
|
|
|
//foreach (var par in pars)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// if (par.DbType != System.Data.DbType.String || par.Value == null) continue;
|
|
|
|
|
|
// if (par.Value.ToString().Length > 100)
|
|
|
|
|
|
// par.Value = string.Concat(par.Value.ToString()[..100], "......");
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
2024-07-17 17:37:06 +08:00
|
|
|
|
var log = $"【{DateTime.Now}——执行SQL】\r\n{UtilMethods.GetNativeSql(sql, pars)}\r\n";
|
2024-07-04 17:15:52 +08:00
|
|
|
|
var originColor = Console.ForegroundColor;
|
|
|
|
|
|
if (sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
|
|
|
|
if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
|
|
|
|
if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase))
|
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
|
Console.WriteLine(log);
|
|
|
|
|
|
Console.ForegroundColor = originColor;
|
|
|
|
|
|
App.PrintToMiniProfiler("SqlSugar", "Info", log);
|
|
|
|
|
|
};
|
|
|
|
|
|
}
|
2024-12-17 11:52:40 +08:00
|
|
|
|
db.Aop.OnLogExecuted = (sql, pars) =>
|
|
|
|
|
|
{
|
|
|
|
|
|
//// 若参数值超过100个字符则进行截取
|
|
|
|
|
|
//foreach (var par in pars)
|
|
|
|
|
|
//{
|
|
|
|
|
|
// if (par.DbType != System.Data.DbType.String || par.Value == null) continue;
|
|
|
|
|
|
// if (par.Value.ToString().Length > 100)
|
|
|
|
|
|
// par.Value = string.Concat(par.Value.ToString()[..100], "......");
|
|
|
|
|
|
//}
|
|
|
|
|
|
|
|
|
|
|
|
// 执行时间超过5秒时
|
|
|
|
|
|
if (db.Ado.SqlExecutionTime.TotalSeconds <= 5) return;
|
|
|
|
|
|
|
|
|
|
|
|
var fileName = db.Ado.SqlStackTrace.FirstFileName; // 文件名
|
|
|
|
|
|
var fileLine = db.Ado.SqlStackTrace.FirstLine; // 行号
|
|
|
|
|
|
var firstMethodName = db.Ado.SqlStackTrace.FirstMethodName; // 方法名
|
|
|
|
|
|
var log = $"【{DateTime.Now}——超时SQL】\r\n【所在文件名】:{fileName}\r\n【代码行数】:{fileLine}\r\n【方法名】:{firstMethodName}\r\n" + $"【SQL语句】:{UtilMethods.GetNativeSql(sql, pars)}";
|
|
|
|
|
|
Log.Warning(log);
|
|
|
|
|
|
App.PrintToMiniProfiler("SqlSugar", "Slow", log);
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2024-07-04 17:15:52 +08:00
|
|
|
|
// 数据审计
|
2024-11-25 21:18:11 +08:00
|
|
|
|
db.Aop.DataExecuting = (oldValue, entityInfo) =>
|
2024-07-04 17:15:52 +08:00
|
|
|
|
{
|
2024-08-08 15:04:39 +08:00
|
|
|
|
// 若正在处理种子数据则直接返回
|
|
|
|
|
|
if (_isHandlingSeedData) return;
|
|
|
|
|
|
|
2024-12-25 23:52:31 +08:00
|
|
|
|
// 新增/插入 操作
|
2024-07-04 17:15:52 +08:00
|
|
|
|
if (entityInfo.OperationType == DataFilterType.InsertByObject)
|
|
|
|
|
|
{
|
2024-12-25 23:52:31 +08:00
|
|
|
|
// 若是主键且非自增类型
|
|
|
|
|
|
if (entityInfo.EntityColumnInfo.IsPrimarykey && !entityInfo.EntityColumnInfo.IsIdentity)
|
2024-07-04 17:15:52 +08:00
|
|
|
|
{
|
2024-12-25 23:52:31 +08:00
|
|
|
|
// 雪花Id类型(长整型且空则赋值)
|
|
|
|
|
|
if (entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long))
|
|
|
|
|
|
{
|
|
|
|
|
|
var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
|
|
|
|
|
|
if (id == null || (long)id == 0)
|
|
|
|
|
|
entityInfo.SetValue(YitIdHelper.NextId());
|
|
|
|
|
|
}
|
|
|
|
|
|
// Guid类型(空则赋值)
|
|
|
|
|
|
else if (entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(Guid))
|
|
|
|
|
|
{
|
|
|
|
|
|
var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
|
|
|
|
|
|
if (id == null)
|
|
|
|
|
|
entityInfo.SetValue(Guid.NewGuid());
|
|
|
|
|
|
}
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 若创建时间为空则赋值当前时间
|
2024-07-17 10:45:38 +08:00
|
|
|
|
else if (entityInfo.PropertyName == nameof(EntityBase.CreateTime))
|
2024-07-04 17:15:52 +08:00
|
|
|
|
{
|
2024-07-17 18:30:44 +08:00
|
|
|
|
var createTime = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue)!;
|
2024-07-18 11:28:17 +08:00
|
|
|
|
if (createTime == null || createTime.Equals(DateTime.MinValue))
|
2024-07-17 17:37:06 +08:00
|
|
|
|
entityInfo.SetValue(DateTime.Now);
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
// 若当前用户非空(web线程时)
|
2024-11-25 16:27:14 +08:00
|
|
|
|
if (App.User == null) return;
|
2024-11-25 21:18:11 +08:00
|
|
|
|
|
2024-11-25 16:27:14 +08:00
|
|
|
|
dynamic entityValue = entityInfo.EntityValue;
|
|
|
|
|
|
if (entityInfo.PropertyName == nameof(EntityTenantId.TenantId))
|
2024-07-04 17:15:52 +08:00
|
|
|
|
{
|
2024-11-25 16:27:14 +08:00
|
|
|
|
if (entityValue.TenantId == null || entityValue.TenantId == 0)
|
|
|
|
|
|
entityInfo.SetValue(App.User.FindFirst(ClaimConst.TenantId)?.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (entityInfo.PropertyName == nameof(EntityBase.CreateUserId))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (entityValue.CreateUserId == null || entityValue.CreateUserId == 0)
|
|
|
|
|
|
entityInfo.SetValue(App.User.FindFirst(ClaimConst.UserId)?.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (entityInfo.PropertyName == nameof(EntityBase.CreateUserName))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(entityValue.CreateUserName))
|
|
|
|
|
|
entityInfo.SetValue(App.User.FindFirst(ClaimConst.RealName)?.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (entityInfo.PropertyName == nameof(EntityBaseData.CreateOrgId))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (entityValue.CreateOrgId == null || entityValue.CreateOrgId == 0)
|
|
|
|
|
|
entityInfo.SetValue(App.User.FindFirst(ClaimConst.OrgId)?.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (entityInfo.PropertyName == nameof(EntityBaseData.CreateOrgName))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(entityValue.CreateOrgName))
|
|
|
|
|
|
entityInfo.SetValue(App.User.FindFirst(ClaimConst.OrgName)?.Value);
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-12-25 23:52:31 +08:00
|
|
|
|
// 编辑/更新 操作
|
2024-07-04 17:15:52 +08:00
|
|
|
|
else if (entityInfo.OperationType == DataFilterType.UpdateByObject)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (entityInfo.PropertyName == nameof(EntityBase.UpdateTime))
|
2024-07-17 17:37:06 +08:00
|
|
|
|
entityInfo.SetValue(DateTime.Now);
|
2024-07-04 17:15:52 +08:00
|
|
|
|
else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserId))
|
|
|
|
|
|
entityInfo.SetValue(App.User?.FindFirst(ClaimConst.UserId)?.Value);
|
|
|
|
|
|
else if (entityInfo.PropertyName == nameof(EntityBase.UpdateUserName))
|
|
|
|
|
|
entityInfo.SetValue(App.User?.FindFirst(ClaimConst.RealName)?.Value);
|
|
|
|
|
|
}
|
2024-11-25 21:18:11 +08:00
|
|
|
|
|
|
|
|
|
|
//// 自定义数据审计
|
|
|
|
|
|
//SqlSugarDataExecuting.Execute(oldValue, entityInfo);
|
2024-07-04 17:15:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 超管排除其他过滤器
|
|
|
|
|
|
if (App.User?.FindFirst(ClaimConst.AccountType)?.Value == ((int)AccountTypeEnum.SuperAdmin).ToString())
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// 配置假删除过滤器
|
|
|
|
|
|
db.QueryFilter.AddTableFilter<IDeletedFilter>(u => u.IsDelete == false);
|
|
|
|
|
|
|
|
|
|
|
|
// 配置租户过滤器
|
|
|
|
|
|
var tenantId = App.User?.FindFirst(ClaimConst.TenantId)?.Value;
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(tenantId))
|
|
|
|
|
|
db.QueryFilter.AddTableFilter<ITenantIdFilter>(u => u.TenantId == long.Parse(tenantId));
|
|
|
|
|
|
|
|
|
|
|
|
// 配置用户机构(数据范围)过滤器
|
|
|
|
|
|
SqlSugarFilter.SetOrgEntityFilter(db);
|
|
|
|
|
|
|
|
|
|
|
|
// 配置自定义过滤器
|
|
|
|
|
|
SqlSugarFilter.SetCustomEntityFilter(db);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 开启库表差异化日志
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="db"></param>
|
|
|
|
|
|
/// <param name="config"></param>
|
|
|
|
|
|
private static void SetDbDiffLog(SqlSugarScopeProvider db, DbConnectionConfig config)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!config.DbSettings.EnableDiffLog) return;
|
|
|
|
|
|
|
|
|
|
|
|
db.Aop.OnDiffLogEvent = async u =>
|
|
|
|
|
|
{
|
2024-11-30 03:12:36 +08:00
|
|
|
|
// 记录差异数据
|
|
|
|
|
|
var diffData = new List<dynamic>();
|
2024-10-29 21:41:53 +08:00
|
|
|
|
for (int i = 0; i < u.AfterData.Count; i++)
|
|
|
|
|
|
{
|
2024-11-30 03:12:36 +08:00
|
|
|
|
var diffColumns = new List<dynamic>();
|
2024-10-29 21:41:53 +08:00
|
|
|
|
var afterColumns = u.AfterData[i].Columns;
|
|
|
|
|
|
var beforeColumns = u.BeforeData[i].Columns;
|
|
|
|
|
|
for (int j = 0; j < afterColumns.Count; j++)
|
|
|
|
|
|
{
|
2024-11-30 03:12:36 +08:00
|
|
|
|
if (afterColumns[j].Value.Equals(beforeColumns[j].Value)) continue;
|
|
|
|
|
|
diffColumns.Add(new
|
|
|
|
|
|
{
|
|
|
|
|
|
afterColumns[j].IsPrimaryKey,
|
|
|
|
|
|
afterColumns[j].ColumnName,
|
|
|
|
|
|
afterColumns[j].ColumnDescription,
|
|
|
|
|
|
BeforeValue = beforeColumns[j].Value,
|
|
|
|
|
|
AfterValue = afterColumns[j].Value,
|
|
|
|
|
|
});
|
2024-10-29 21:41:53 +08:00
|
|
|
|
}
|
2024-11-30 03:12:36 +08:00
|
|
|
|
diffData.Add(new
|
|
|
|
|
|
{
|
|
|
|
|
|
u.AfterData[i].TableName,
|
|
|
|
|
|
u.AfterData[i].TableDescription,
|
|
|
|
|
|
Columns = diffColumns
|
|
|
|
|
|
});
|
2024-10-29 21:41:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-04 17:15:52 +08:00
|
|
|
|
var logDiff = new SysLogDiff
|
|
|
|
|
|
{
|
2024-11-30 03:12:36 +08:00
|
|
|
|
// 差异数据(字段描述、列名、值、表名、表描述)
|
|
|
|
|
|
DiffData = JSON.Serialize(diffData),
|
2024-07-04 17:15:52 +08:00
|
|
|
|
// 传进来的对象(如果对象为空,则使用首个数据的表名作为业务对象)
|
|
|
|
|
|
BusinessData = u.BusinessData == null ? u.AfterData.FirstOrDefault()?.TableName : JSON.Serialize(u.BusinessData),
|
|
|
|
|
|
// 枚举(insert、update、delete)
|
|
|
|
|
|
DiffType = u.DiffType.ToString(),
|
2024-11-30 03:12:36 +08:00
|
|
|
|
Sql = u.Sql,
|
|
|
|
|
|
Parameters = JSON.Serialize(u.Parameters.Select(e => new { e.ParameterName, e.Value, TypeName = e.DbType.ToString() })),
|
2024-07-04 17:15:52 +08:00
|
|
|
|
Elapsed = u.Time == null ? 0 : (long)u.Time.Value.TotalMilliseconds
|
|
|
|
|
|
};
|
2024-11-25 16:27:14 +08:00
|
|
|
|
var logDb = ITenant.IsAnyConnection(SqlSugarConst.LogConfigId) ? ITenant.GetConnectionScope(SqlSugarConst.LogConfigId) : ITenant.GetConnectionScope(SqlSugarConst.MainConfigId);
|
2024-07-04 17:15:52 +08:00
|
|
|
|
await logDb.CopyNew().Insertable(logDiff).ExecuteCommandAsync();
|
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
2024-07-17 17:37:06 +08:00
|
|
|
|
Console.WriteLine(DateTime.Now + $"\r\n*****开始差异日志*****\r\n{Environment.NewLine}{JSON.Serialize(logDiff)}{Environment.NewLine}*****结束差异日志*****\r\n");
|
2024-07-04 17:15:52 +08:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化数据库
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="db"></param>
|
|
|
|
|
|
/// <param name="config"></param>
|
|
|
|
|
|
private static void InitDatabase(SqlSugarScope db, DbConnectionConfig config)
|
|
|
|
|
|
{
|
|
|
|
|
|
SqlSugarScopeProvider dbProvider = db.GetConnectionScope(config.ConfigId);
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化/创建数据库
|
|
|
|
|
|
if (config.DbSettings.EnableInitDb)
|
|
|
|
|
|
{
|
2024-07-21 17:08:33 +08:00
|
|
|
|
Log.Information($"初始化数据库 {config.DbType} - {config.ConfigId} - {config.ConnectionString}");
|
2024-11-25 16:27:14 +08:00
|
|
|
|
if (config.DbType != SqlSugar.DbType.Oracle) dbProvider.DbMaintenance.CreateDatabase();
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化表结构
|
|
|
|
|
|
if (config.TableSettings.EnableInitTable)
|
|
|
|
|
|
{
|
2024-07-21 17:08:33 +08:00
|
|
|
|
Log.Information($"初始化表结构 {config.DbType} - {config.ConfigId}");
|
2024-07-04 17:15:52 +08:00
|
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
|
|
if (config.ConfigId.ToString() == SqlSugarConst.MainConfigId) // 默认库(有系统表特性、没有日志表和租户表特性)
|
|
|
|
|
|
entityTypes = entityTypes.Where(u => u.GetCustomAttributes<SysTableAttribute>().Any() || (!u.GetCustomAttributes<LogTableAttribute>().Any() && !u.GetCustomAttributes<TenantAttribute>().Any())).ToList();
|
|
|
|
|
|
else if (config.ConfigId.ToString() == SqlSugarConst.LogConfigId) // 日志库
|
|
|
|
|
|
entityTypes = entityTypes.Where(u => u.GetCustomAttributes<LogTableAttribute>().Any()).ToList();
|
|
|
|
|
|
else
|
|
|
|
|
|
entityTypes = entityTypes.Where(u => u.GetCustomAttribute<TenantAttribute>()?.configId.ToString() == config.ConfigId.ToString()).ToList(); // 自定义的库
|
2024-07-21 17:08:33 +08:00
|
|
|
|
|
|
|
|
|
|
int count = 0, sum = entityTypes.Count;
|
2024-11-05 20:37:44 +08:00
|
|
|
|
foreach (var entityType in entityTypes)
|
2024-07-04 17:15:52 +08:00
|
|
|
|
{
|
2024-11-05 20:37:44 +08:00
|
|
|
|
Console.WriteLine($"创建表 {entityType} ({config.ConfigId} - {++count}/{sum})");
|
|
|
|
|
|
if (entityType.GetCustomAttribute<SplitTableAttribute>() == null)
|
|
|
|
|
|
dbProvider.CodeFirst.InitTables(entityType);
|
|
|
|
|
|
else
|
|
|
|
|
|
dbProvider.CodeFirst.SplitTables().InitTables(entityType);
|
|
|
|
|
|
}
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化种子数据
|
2024-11-25 16:27:14 +08:00
|
|
|
|
if (config.SeedSettings.EnableInitSeed) InitSeedData(db, config);
|
|
|
|
|
|
}
|
2024-08-08 15:04:39 +08:00
|
|
|
|
|
2024-11-25 16:27:14 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化种子数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="db"></param>
|
|
|
|
|
|
/// <param name="config"></param>
|
|
|
|
|
|
private static void InitSeedData(SqlSugarScope db, DbConnectionConfig config)
|
|
|
|
|
|
{
|
|
|
|
|
|
SqlSugarScopeProvider dbProvider = db.GetConnectionScope(config.ConfigId);
|
|
|
|
|
|
_isHandlingSeedData = true;
|
2024-07-21 17:08:33 +08:00
|
|
|
|
|
2024-11-25 16:27:14 +08:00
|
|
|
|
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<>))))
|
|
|
|
|
|
.Where(u => !u.IsDefined(typeof(TenantSeedAttribute), false))
|
|
|
|
|
|
.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();
|
|
|
|
|
|
if (config.ConfigId.ToString() == SqlSugarConst.MainConfigId) // 默认库(有系统表特性、没有日志表和租户表特性)
|
2024-07-04 17:15:52 +08:00
|
|
|
|
{
|
2024-11-25 16:27:14 +08:00
|
|
|
|
if (entityType.GetCustomAttribute<SysTableAttribute>() == null && (entityType.GetCustomAttribute<LogTableAttribute>() != null || entityType.GetCustomAttribute<TenantAttribute>() != null))
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
else if (config.ConfigId.ToString() == SqlSugarConst.LogConfigId) // 日志库
|
|
|
|
|
|
{
|
|
|
|
|
|
if (entityType.GetCustomAttribute<LogTableAttribute>() == null)
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
var att = entityType.GetCustomAttribute<TenantAttribute>(); // 自定义的库
|
|
|
|
|
|
if (att == null || att.configId.ToString() != config.ConfigId.ToString()) continue;
|
|
|
|
|
|
}
|
2024-07-04 17:15:52 +08:00
|
|
|
|
|
2024-11-25 16:27:14 +08:00
|
|
|
|
var instance = Activator.CreateInstance(seedType);
|
|
|
|
|
|
var hasDataMethod = seedType.GetMethod("HasData");
|
|
|
|
|
|
var seedData = ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>();
|
|
|
|
|
|
if (seedData == null) continue;
|
2024-07-04 17:15:52 +08:00
|
|
|
|
|
2024-11-25 16:27:14 +08:00
|
|
|
|
var entityInfo = dbProvider.EntityMaintenance.GetEntityInfo(entityType);
|
|
|
|
|
|
Console.WriteLine($"添加数据 {entityInfo.DbTableName} ({config.ConfigId} - {++count}/{sum},数据量:{seedData.Count()})");
|
2024-07-21 16:32:44 +08:00
|
|
|
|
|
2024-11-25 16:27:14 +08:00
|
|
|
|
// 若实体包含Id字段,则设置为当前租户Id递增1
|
|
|
|
|
|
if (entityInfo.Columns.Any(u => u.PropertyName == nameof(EntityBaseId.Id)))
|
|
|
|
|
|
{
|
|
|
|
|
|
var seedId = config.ConfigId.ToLong();
|
|
|
|
|
|
foreach (var sd in seedData)
|
2024-09-19 18:55:56 +08:00
|
|
|
|
{
|
2024-11-25 16:27:14 +08:00
|
|
|
|
var id = sd.GetType().GetProperty(nameof(EntityBaseId.Id))!.GetValue(sd, null);
|
|
|
|
|
|
if (id != null && (id.ToString() == "0" || string.IsNullOrWhiteSpace(id.ToString())))
|
|
|
|
|
|
sd.GetType().GetProperty(nameof(EntityBaseId.Id))!.SetValue(sd, ++seedId);
|
2024-09-19 18:55:56 +08:00
|
|
|
|
}
|
2024-11-25 16:27:14 +08:00
|
|
|
|
}
|
2024-09-20 01:15:51 +08:00
|
|
|
|
|
2024-11-25 16:27:14 +08:00
|
|
|
|
if (entityType.GetCustomAttribute<SplitTableAttribute>(true) != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 拆分表的操作需要实体类型,而通过反射很难实现
|
|
|
|
|
|
// 所以,这里将Init方法写在“种子数据类”内部,再传入 db 反射调用
|
|
|
|
|
|
var hasInitMethod = seedType.GetMethod("Init");
|
|
|
|
|
|
var parameters = new object[] { db };
|
|
|
|
|
|
hasInitMethod?.Invoke(instance, parameters);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (entityInfo.Columns.Any(u => u.IsPrimarykey))
|
2024-07-04 17:15:52 +08:00
|
|
|
|
{
|
2024-11-25 16:27:14 +08:00
|
|
|
|
// 按主键进行批量增加和更新
|
|
|
|
|
|
var storage = dbProvider.StorageableByObject(seedData.ToList()).ToStorage();
|
|
|
|
|
|
|
|
|
|
|
|
// 先修改再插入,否则会更新修改时间字段
|
|
|
|
|
|
if (seedType.GetCustomAttribute<IgnoreUpdateSeedAttribute>() == null) // 有忽略更新种子特性时则不更新
|
|
|
|
|
|
{
|
|
|
|
|
|
int updateCount = storage.AsUpdateable.IgnoreColumns(entityInfo.Columns.Where(u => u.PropertyInfo.GetCustomAttribute<IgnoreUpdateSeedColumnAttribute>() != null).Select(u => u.PropertyName).ToArray()).ExecuteCommand();
|
|
|
|
|
|
Console.WriteLine($" 修改 {updateCount}/{seedData.Count()} 条记录");
|
|
|
|
|
|
}
|
|
|
|
|
|
int insertCount = storage.AsInsertable.ExecuteCommand();
|
|
|
|
|
|
Console.WriteLine($" 插入 {insertCount}/{seedData.Count()} 条记录");
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2024-11-25 16:27:14 +08:00
|
|
|
|
// 无主键则只进行插入
|
|
|
|
|
|
if (!dbProvider.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any())
|
|
|
|
|
|
dbProvider.InsertableByObject(seedData.ToList()).ExecuteCommand();
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-25 16:27:14 +08:00
|
|
|
|
|
2024-11-25 21:18:11 +08:00
|
|
|
|
_isHandlingSeedData = false;
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化租户业务数据库
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="config"></param>
|
2024-08-16 20:05:43 +08:00
|
|
|
|
public static void InitTenantDatabase(DbConnectionConfig config)
|
2024-07-04 17:15:52 +08:00
|
|
|
|
{
|
|
|
|
|
|
SetDbConfig(config);
|
|
|
|
|
|
|
2024-08-16 20:05:43 +08:00
|
|
|
|
var iTenant = App.GetRequiredService<ISqlSugarClient>().AsTenant();
|
2024-07-04 17:15:52 +08:00
|
|
|
|
if (!iTenant.IsAnyConnection(config.ConfigId.ToString()))
|
|
|
|
|
|
iTenant.AddConnection(config);
|
|
|
|
|
|
var db = iTenant.GetConnectionScope(config.ConfigId.ToString());
|
|
|
|
|
|
db.DbMaintenance.CreateDatabase();
|
|
|
|
|
|
|
|
|
|
|
|
// 初始化租户库表结构-获取所有业务应用表(排除系统表、日志表、特定库表)
|
|
|
|
|
|
var entityTypes = App.EffectiveTypes
|
|
|
|
|
|
.Where(u => !u.GetCustomAttributes<IgnoreTableAttribute>().Any())
|
|
|
|
|
|
.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.IsDefined(typeof(SugarTable), false) &&
|
|
|
|
|
|
!u.IsDefined(typeof(SysTableAttribute), false) && !u.IsDefined(typeof(LogTableAttribute), false) && !u.IsDefined(typeof(TenantAttribute), false)).ToList();
|
2024-07-18 17:49:22 +08:00
|
|
|
|
if (entityTypes.Count == 0) return;
|
2024-07-04 17:15:52 +08:00
|
|
|
|
|
|
|
|
|
|
foreach (var entityType in entityTypes)
|
|
|
|
|
|
{
|
|
|
|
|
|
var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>();
|
|
|
|
|
|
if (splitTable == null)
|
|
|
|
|
|
db.CodeFirst.InitTables(entityType);
|
|
|
|
|
|
else
|
|
|
|
|
|
db.CodeFirst.SplitTables().InitTables(entityType);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-09-06 12:57:09 +08:00
|
|
|
|
// 初始化租户业务数据
|
2024-09-06 13:41:23 +08:00
|
|
|
|
InitTenantData(iTenant, config.ConfigId.ToLong(), config.ConfigId.ToLong());
|
2024-08-16 20:05:43 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// 初始化租户业务数据
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="iTenant"></param>
|
2024-09-06 13:41:23 +08:00
|
|
|
|
/// <param name="dbConfigId">库标识</param>
|
|
|
|
|
|
/// <param name="tenantId">租户Id</param>
|
|
|
|
|
|
public static void InitTenantData(ITenant iTenant, long dbConfigId, long tenantId)
|
2024-08-16 20:05:43 +08:00
|
|
|
|
{
|
2024-07-04 17:15:52 +08:00
|
|
|
|
var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))))
|
2024-09-06 12:55:27 +08:00
|
|
|
|
.Where(u => u.IsDefined(typeof(TenantSeedAttribute), false))
|
2024-07-17 18:30:44 +08:00
|
|
|
|
.OrderBy(u => u.GetCustomAttributes(typeof(SeedDataAttribute), false).Length > 0 ? ((SeedDataAttribute)u.GetCustomAttributes(typeof(SeedDataAttribute), false)[0]).Order : 0).ToList();
|
2024-08-16 20:05:43 +08:00
|
|
|
|
if (seedDataTypes == null || seedDataTypes.Count < 1) return;
|
2024-07-04 17:15:52 +08:00
|
|
|
|
|
2024-08-16 20:05:43 +08:00
|
|
|
|
var db = iTenant.GetConnectionScope(dbConfigId);
|
2024-07-04 17:15:52 +08:00
|
|
|
|
foreach (var seedType in seedDataTypes)
|
|
|
|
|
|
{
|
|
|
|
|
|
var instance = Activator.CreateInstance(seedType);
|
|
|
|
|
|
var hasDataMethod = seedType.GetMethod("HasData");
|
2024-12-30 02:20:39 +08:00
|
|
|
|
var seedData = ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>().ToList() ?? [];
|
2024-07-17 18:30:44 +08:00
|
|
|
|
if (seedData.Count == 0) continue;
|
2024-07-04 17:15:52 +08:00
|
|
|
|
|
|
|
|
|
|
var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
|
|
|
|
|
|
var entityInfo = db.EntityMaintenance.GetEntityInfo(entityType);
|
2024-08-16 20:05:43 +08:00
|
|
|
|
// var dbConfigId = config.ConfigId.ToLong();
|
2024-07-04 17:15:52 +08:00
|
|
|
|
// 若实体包含租户Id字段,则设置为当前租户Id
|
|
|
|
|
|
if (entityInfo.Columns.Any(u => u.PropertyName == nameof(EntityTenantId.TenantId)))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var sd in seedData)
|
|
|
|
|
|
{
|
2024-09-06 13:41:23 +08:00
|
|
|
|
sd.GetType().GetProperty(nameof(EntityTenantId.TenantId))!.SetValue(sd, tenantId);
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 若实体包含Pid字段,则设置为当前租户Id
|
|
|
|
|
|
if (entityInfo.Columns.Any(u => u.PropertyName == nameof(SysOrg.Pid)))
|
|
|
|
|
|
{
|
|
|
|
|
|
foreach (var sd in seedData)
|
|
|
|
|
|
{
|
2024-09-06 13:41:23 +08:00
|
|
|
|
sd.GetType().GetProperty(nameof(SysOrg.Pid))!.SetValue(sd, tenantId);
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
// 若实体包含Id字段,则设置为当前租户Id递增1
|
|
|
|
|
|
if (entityInfo.Columns.Any(u => u.PropertyName == nameof(EntityBaseId.Id)))
|
|
|
|
|
|
{
|
2024-09-19 15:09:19 +08:00
|
|
|
|
var seedId = tenantId;
|
2024-07-04 17:15:52 +08:00
|
|
|
|
foreach (var sd in seedData)
|
|
|
|
|
|
{
|
2024-09-19 14:47:52 +08:00
|
|
|
|
var id = sd.GetType().GetProperty(nameof(EntityBaseId.Id))!.GetValue(sd, null);
|
|
|
|
|
|
if (id != null && (id.ToString() == "0" || string.IsNullOrWhiteSpace(id.ToString())))
|
2024-09-19 15:09:19 +08:00
|
|
|
|
sd.GetType().GetProperty(nameof(EntityBaseId.Id))!.SetValue(sd, ++seedId);
|
2024-07-04 17:15:52 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 若实体是系统内置,则切换至默认库
|
|
|
|
|
|
if (entityType.GetCustomAttribute<SysTableAttribute>() != null)
|
|
|
|
|
|
db = iTenant.GetConnectionScope(SqlSugarConst.MainConfigId);
|
|
|
|
|
|
|
|
|
|
|
|
if (entityInfo.Columns.Any(u => u.IsPrimarykey))
|
|
|
|
|
|
{
|
|
|
|
|
|
// 按主键进行批量增加和更新
|
|
|
|
|
|
var storage = db.StorageableByObject(seedData).ToStorage();
|
|
|
|
|
|
storage.AsInsertable.ExecuteCommand();
|
|
|
|
|
|
if (seedType.GetCustomAttribute<IgnoreUpdateSeedAttribute>() == null) // 有忽略更新种子特性时则不更新
|
|
|
|
|
|
storage.AsUpdateable.IgnoreColumns(entityInfo.Columns.Where(c => c.PropertyInfo.GetCustomAttribute<IgnoreUpdateSeedColumnAttribute>() != null).Select(c => c.PropertyName).ToArray()).ExecuteCommand();
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// 无主键则只进行插入
|
|
|
|
|
|
if (!db.Queryable(entityInfo.DbTableName, entityInfo.DbTableName).Any())
|
|
|
|
|
|
db.InsertableByObject(seedData).ExecuteCommand();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-11-05 20:37:44 +08:00
|
|
|
|
}
|