SqlSugarSetup.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. using Masuit.Tools;
  2. namespace Admin.NET.Core;
  3. public static class SqlSugarSetup
  4. {
  5. /// <summary>
  6. /// Sqlsugar 上下文初始化
  7. /// </summary>
  8. /// <param name="services"></param>
  9. public static void AddSqlSugar(this IServiceCollection services)
  10. {
  11. var dbOptions = App.GetOptions<DbConnectionOptions>();
  12. dbOptions.ConnectionConfigs.ForEach(config =>
  13. {
  14. SetDbConfig(config);
  15. });
  16. SqlSugarScope sqlSugar = new(dbOptions.ConnectionConfigs.Adapt<List<ConnectionConfig>>(), client =>
  17. {
  18. dbOptions.ConnectionConfigs.ForEach(config =>
  19. {
  20. var db = client.GetConnectionScope((string)config.ConfigId);
  21. SetDbAop(db, config);
  22. });
  23. });
  24. // 初始化数据库表结构及种子数据
  25. InitDataBase(sqlSugar, dbOptions);
  26. services.AddSingleton<ISqlSugarClient>(sqlSugar); // 单例注册
  27. services.AddScoped(typeof(SqlSugarRepository<>)); // 仓储注册
  28. services.AddUnitOfWork<SqlSugarUnitOfWork>(); // 注册事务与工作单元
  29. }
  30. /// <summary>
  31. /// 配置连接属性
  32. /// </summary>
  33. /// <param name="config"></param>
  34. public static void SetDbConfig(DbConnectionConfig config)
  35. {
  36. var configureExternalServices = new ConfigureExternalServices
  37. {
  38. EntityService = (type, column) => // 修改列可空-1、带?问号 2、String类型若没有Required
  39. {
  40. if ((type.PropertyType.IsGenericType && type.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
  41. || (type.PropertyType == typeof(string) && type.GetCustomAttribute<RequiredAttribute>() == null))
  42. column.IsNullable = true;
  43. },
  44. DataInfoCacheService = new SqlSugarCache(),
  45. };
  46. config.ConfigureExternalServices = configureExternalServices;
  47. config.InitKeyType = InitKeyType.Attribute;
  48. config.IsAutoCloseConnection = true;
  49. config.MoreSettings = new ConnMoreSettings
  50. {
  51. IsAutoRemoveDataCache = true
  52. };
  53. }
  54. /// <summary>
  55. /// 配置Aop
  56. /// </summary>
  57. /// <param name="db"></param>
  58. /// <param name="config"></param>
  59. public static void SetDbAop(this SqlSugarScopeProvider db, DbConnectionConfig config)
  60. {
  61. // 设置超时时间
  62. db.Ado.CommandTimeOut = 30;
  63. // 打印SQL语句
  64. db.Aop.OnLogExecuting = (sql, pars) =>
  65. {
  66. if (sql.StartsWith("SELECT", StringComparison.OrdinalIgnoreCase))
  67. Console.ForegroundColor = ConsoleColor.Green;
  68. if (sql.StartsWith("UPDATE", StringComparison.OrdinalIgnoreCase) || sql.StartsWith("INSERT", StringComparison.OrdinalIgnoreCase))
  69. Console.ForegroundColor = ConsoleColor.White;
  70. if (sql.StartsWith("DELETE", StringComparison.OrdinalIgnoreCase))
  71. Console.ForegroundColor = ConsoleColor.Blue;
  72. Console.WriteLine("【" + DateTime.Now + "——执行SQL】\r\n" + UtilMethods.GetSqlString(config.DbType, sql, pars) + "\r\n");
  73. App.PrintToMiniProfiler("SqlSugar", "Info", sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value)));
  74. };
  75. db.Aop.OnError = (ex) =>
  76. {
  77. if (ex.Parametres == null) return;
  78. Console.ForegroundColor = ConsoleColor.Red;
  79. var pars = db.Utilities.SerializeObject(((SugarParameter[])ex.Parametres).ToDictionary(it => it.ParameterName, it => it.Value));
  80. Console.WriteLine("【" + DateTime.Now + "——错误SQL】\r\n" + UtilMethods.GetSqlString(config.DbType, ex.Sql, (SugarParameter[])ex.Parametres) + "\r\n");
  81. App.PrintToMiniProfiler("SqlSugar", "Error", $"{ex.Message}{Environment.NewLine}{ex.Sql}{pars}{Environment.NewLine}");
  82. };
  83. // 数据审计
  84. db.Aop.DataExecuting = (oldValue, entityInfo) =>
  85. {
  86. // 新增操作
  87. if (entityInfo.OperationType == DataFilterType.InsertByObject)
  88. {
  89. // 主键(long类型)且没有值的---赋值雪花Id
  90. if (entityInfo.EntityColumnInfo.IsPrimarykey && entityInfo.EntityColumnInfo.PropertyInfo.PropertyType == typeof(long))
  91. {
  92. var id = entityInfo.EntityColumnInfo.PropertyInfo.GetValue(entityInfo.EntityValue);
  93. if (id == null || (long)id == 0)
  94. entityInfo.SetValue(Yitter.IdGenerator.YitIdHelper.NextId());
  95. }
  96. if (entityInfo.PropertyName == "CreateTime")
  97. entityInfo.SetValue(DateTime.Now);
  98. if (App.User != null)
  99. {
  100. if (entityInfo.PropertyName == "TenantId")
  101. {
  102. var tenantId = ((dynamic)entityInfo.EntityValue).TenantId;
  103. if (tenantId == null || tenantId == 0)
  104. entityInfo.SetValue(App.User.FindFirst(ClaimConst.TenantId)?.Value);
  105. }
  106. if (entityInfo.PropertyName == "CreateUserId")
  107. entityInfo.SetValue(App.User.FindFirst(ClaimConst.UserId)?.Value);
  108. if (entityInfo.PropertyName == "CreateOrgId")
  109. entityInfo.SetValue(App.User.FindFirst(ClaimConst.OrgId)?.Value);
  110. }
  111. }
  112. // 更新操作
  113. if (entityInfo.OperationType == DataFilterType.UpdateByObject)
  114. {
  115. if (entityInfo.PropertyName == "UpdateTime")
  116. entityInfo.SetValue(DateTime.Now);
  117. if (entityInfo.PropertyName == "UpdateUserId")
  118. entityInfo.SetValue(App.User?.FindFirst(ClaimConst.UserId)?.Value);
  119. }
  120. };
  121. //// 差异日志
  122. //db.Aop.OnDiffLogEvent = async u =>
  123. //{
  124. // if (!config.EnableDiffLog) return;
  125. // var LogDiff = new SysLogDiff
  126. // {
  127. // // 操作后记录(字段描述、列名、值、表名、表描述)
  128. // AfterData = JsonConvert.SerializeObject(u.AfterData),
  129. // // 操作前记录(字段描述、列名、值、表名、表描述)
  130. // BeforeData = JsonConvert.SerializeObject(u.BeforeData),
  131. // // 传进来的对象
  132. // BusinessData = JsonConvert.SerializeObject(u.BusinessData),
  133. // // 枚举(insert、update、delete)
  134. // DiffType = u.DiffType.ToString(),
  135. // Sql = UtilMethods.GetSqlString(config.DbType, u.Sql, u.Parameters),
  136. // Parameters = JsonConvert.SerializeObject(u.Parameters),
  137. // Duration = u.Time == null ? 0 : (long)u.Time.Value.TotalMilliseconds
  138. // };
  139. // await client.GetConnectionScope(SqlSugarConst.ConfigId).Insertable(LogDiff).ExecuteCommandAsync();
  140. // Console.ForegroundColor = ConsoleColor.Red;
  141. // Console.WriteLine(DateTime.Now + $"\r\n**********差异日志开始**********\r\n{Environment.NewLine}{JsonConvert.SerializeObject(LogDiff)}{Environment.NewLine}**********差异日志结束**********\r\n");
  142. //};
  143. // 配置实体假删除过滤器
  144. SetDeletedEntityFilter(db);
  145. // 配置租户过滤器
  146. SetTenantEntityFilter(db);
  147. // 配置用户机构范围过滤器
  148. SetOrgEntityFilter(db);
  149. // 配置自定义过滤器
  150. SetCustomEntityFilter(db);
  151. }
  152. /// <summary>
  153. /// 初始化数据库结构
  154. /// </summary>
  155. private static void InitDataBase(SqlSugarScope db, DbConnectionOptions dbOptions)
  156. {
  157. // 创建数据库
  158. dbOptions.ConnectionConfigs.ForEach(config =>
  159. {
  160. if (!config.EnableInitDb || config.DbType == SqlSugar.DbType.Oracle) return;
  161. db.GetConnectionScope(config.ConfigId).DbMaintenance.CreateDatabase();
  162. });
  163. // 获取所有实体表-初始化表结构
  164. var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
  165. && u.IsDefined(typeof(SugarTable), false) && !u.IsDefined(typeof(NotTableAttribute), false));
  166. if (!entityTypes.Any()) return;
  167. foreach (var entityType in entityTypes)
  168. {
  169. var tAtt = entityType.GetCustomAttribute<TenantAttribute>(); // 多数据库
  170. var configId = tAtt == null ? SqlSugarConst.ConfigId : tAtt.configId.ToString();
  171. if (!dbOptions.ConnectionConfigs.FirstOrDefault(u => u.ConfigId == configId).EnableInitDb)
  172. continue;
  173. var db2 = db.GetConnectionScope(configId);
  174. var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>(); // 分表
  175. if (splitTable == null)
  176. db2.CodeFirst.InitTables(entityType);
  177. else
  178. db2.CodeFirst.SplitTables().InitTables(entityType);
  179. }
  180. // 获取所有种子配置-初始化数据
  181. var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
  182. && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))));
  183. if (!seedDataTypes.Any()) return;
  184. foreach (var seedType in seedDataTypes)
  185. {
  186. var instance = Activator.CreateInstance(seedType);
  187. var hasDataMethod = seedType.GetMethod("HasData");
  188. var seedData = ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>();
  189. if (seedData == null) continue;
  190. var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
  191. var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
  192. var configId = tAtt == null ? SqlSugarConst.ConfigId : tAtt.configId.ToString();
  193. if (!dbOptions.ConnectionConfigs.FirstOrDefault(u => u.ConfigId == configId).EnableInitDb)
  194. continue;
  195. var db2 = db.GetConnectionScope(configId);
  196. var seedDataTable = seedData.ToList().ToDataTable();
  197. seedDataTable.TableName = db.EntityMaintenance.GetEntityInfo(entityType).DbTableName;
  198. if (seedDataTable.Columns.Contains(SqlSugarConst.PrimaryKey))
  199. {
  200. var storage = db2.Storageable(seedDataTable).WhereColumns(SqlSugarConst.PrimaryKey).ToStorage();
  201. storage.AsInsertable.ExecuteCommand();
  202. storage.AsUpdateable.ExecuteCommand();
  203. }
  204. else // 没有主键或者不是预定义的主键(没主键有重复的可能)
  205. {
  206. var storage = db2.Storageable(seedDataTable).ToStorage();
  207. storage.AsInsertable.ExecuteCommand();
  208. }
  209. }
  210. }
  211. /// <summary>
  212. /// 初始化数据库结构
  213. /// </summary>
  214. public static void CreateDataBase(ISqlSugarClient db, DbConnectionConfig config, long tenantId)
  215. {
  216. SetDbConfig(config);
  217. var itenant = db.AsTenant();
  218. // 创建数据库
  219. if (!config.EnableInitDb || config.DbType == SqlSugar.DbType.Oracle) return;
  220. itenant.AddConnection(config);
  221. var dbProvider = itenant.GetConnectionScope(config.ConfigId);
  222. SetDbAop(dbProvider, config);
  223. dbProvider.DbMaintenance.CreateDatabase();
  224. // 获取所有实体表-初始化表结构
  225. var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
  226. && u.IsDefined(typeof(SugarTable), false) && !u.IsDefined(typeof(NotTableAttribute), false));
  227. if (!entityTypes.Any()) return;
  228. foreach (var entityType in entityTypes)
  229. {
  230. var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
  231. if (tAtt != null) continue;
  232. var db2 = itenant.GetConnectionScope(config.ConfigId);
  233. var splitTable = entityType.GetCustomAttribute<SplitTableAttribute>();
  234. if (splitTable == null)
  235. db2.CodeFirst.InitTables(entityType);
  236. else
  237. db2.CodeFirst.SplitTables().InitTables(entityType);
  238. }
  239. // 获取所有种子配置-初始化数据
  240. var seedDataTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
  241. && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(ISqlSugarEntitySeedData<>))));
  242. if (!seedDataTypes.Any()) return;
  243. foreach (var seedType in seedDataTypes)
  244. {
  245. var instance = Activator.CreateInstance(seedType);
  246. var hasDataMethod = seedType.GetMethod("HasData");
  247. var seedData = ((IEnumerable)hasDataMethod?.Invoke(instance, null))?.Cast<object>();
  248. if (seedData == null) continue;
  249. var entityType = seedType.GetInterfaces().First().GetGenericArguments().First();
  250. var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
  251. if (tAtt != null) continue;
  252. var db2 = itenant.GetConnectionScope(config.ConfigId);
  253. var seedDataTable = seedData.ToList().ToDataTable();
  254. seedDataTable.TableName = db.EntityMaintenance.GetEntityInfo(entityType).DbTableName;
  255. // 设置租户Id
  256. if (seedDataTable.Columns.Contains(SqlSugarConst.TenantId))
  257. {
  258. foreach (DataRow dr in seedDataTable.Rows)
  259. dr[SqlSugarConst.TenantId] = tenantId;
  260. }
  261. if (seedDataTable.Columns.Contains(SqlSugarConst.PrimaryKey))
  262. {
  263. var storage = db2.Storageable(seedDataTable).WhereColumns(SqlSugarConst.PrimaryKey).ToStorage();
  264. storage.AsInsertable.ExecuteCommand();
  265. storage.AsUpdateable.ExecuteCommand();
  266. }
  267. else // 没有主键或者不是预定义的主键(没主键有重复的可能)
  268. {
  269. var storage = db2.Storageable(seedDataTable).ToStorage();
  270. storage.AsInsertable.ExecuteCommand();
  271. }
  272. }
  273. }
  274. /// <summary>
  275. /// 配置实体假删除过滤器
  276. /// </summary>
  277. private static void SetDeletedEntityFilter(SqlSugarScopeProvider db)
  278. {
  279. // 配置实体假删除缓存
  280. var cacheKey = $"db:{db.CurrentConnectionConfig.ConfigId}:IsDelete";
  281. var tableFilterItemList = db.DataCache.Get<List<TableFilterItem<object>>>(cacheKey);
  282. if (tableFilterItemList == null)
  283. {
  284. // 获取基类实体数据表
  285. var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
  286. && (u.BaseType == typeof(EntityBase) || u.BaseType == typeof(EntityTenant) || u.BaseType == typeof(DataEntityBase)));
  287. if (!entityTypes.Any()) return;
  288. var tableFilterItems = new List<TableFilterItem<object>>();
  289. foreach (var entityType in entityTypes)
  290. {
  291. // 排除非当前数据库实体
  292. var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
  293. if ((tAtt != null && (string)db.CurrentConnectionConfig.ConfigId != tAtt.configId.ToString()) ||
  294. (tAtt == null && (string)db.CurrentConnectionConfig.ConfigId != SqlSugarConst.ConfigId))
  295. continue;
  296. Expression<Func<DataEntityBase, bool>> dynamicExpression = u => u.IsDelete == false;
  297. var tableFilterItem = new TableFilterItem<object>(entityType, dynamicExpression);
  298. tableFilterItems.Add(tableFilterItem);
  299. db.QueryFilter.Add(tableFilterItem);
  300. }
  301. db.DataCache.Add(cacheKey, tableFilterItems);
  302. }
  303. else
  304. {
  305. tableFilterItemList.ForEach(u =>
  306. {
  307. db.QueryFilter.Add(u);
  308. });
  309. }
  310. }
  311. /// <summary>
  312. /// 配置租户过滤器
  313. /// </summary>
  314. private static void SetTenantEntityFilter(SqlSugarScopeProvider db)
  315. {
  316. var tenantId = App.User?.FindFirst(ClaimConst.TenantId)?.Value;
  317. if (string.IsNullOrWhiteSpace(tenantId)) return;
  318. // 配置租户缓存
  319. var cacheKey = $"db:{db.CurrentConnectionConfig.ConfigId}:TenantId:{tenantId}";
  320. var tableFilterItemList = db.DataCache.Get<List<TableFilterItem<object>>>(cacheKey);
  321. if (tableFilterItemList == null)
  322. {
  323. // 获取租户实体数据表
  324. var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
  325. && u.BaseType == typeof(EntityTenant));
  326. if (!entityTypes.Any()) return;
  327. var tableFilterItems = new List<TableFilterItem<object>>();
  328. foreach (var entityType in entityTypes)
  329. {
  330. // 排除非当前数据库实体
  331. var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
  332. if ((tAtt != null && (string)db.CurrentConnectionConfig.ConfigId != tAtt.configId.ToString()) ||
  333. (tAtt == null && (string)db.CurrentConnectionConfig.ConfigId != SqlSugarConst.ConfigId))
  334. continue;
  335. Expression<Func<EntityTenant, bool>> dynamicExpression = u => u.TenantId == long.Parse(tenantId);
  336. var tableFilterItem = new TableFilterItem<object>(entityType, dynamicExpression);
  337. tableFilterItems.Add(tableFilterItem);
  338. db.QueryFilter.Add(tableFilterItem);
  339. }
  340. db.DataCache.Add(cacheKey, tableFilterItems);
  341. }
  342. else
  343. {
  344. tableFilterItemList.ForEach(u =>
  345. {
  346. db.QueryFilter.Add(u);
  347. });
  348. }
  349. }
  350. /// <summary>
  351. /// 配置用户机构范围过滤器
  352. /// </summary>
  353. private static void SetOrgEntityFilter(SqlSugarScopeProvider db)
  354. {
  355. var userId = App.User?.FindFirst(ClaimConst.UserId)?.Value;
  356. if (string.IsNullOrWhiteSpace(userId)) return;
  357. // 配置用户机构范围缓存
  358. var cacheKey = $"db:{db.CurrentConnectionConfig.ConfigId}:UserId:{userId}";
  359. var tableFilterItemList = db.DataCache.Get<List<TableFilterItem<object>>>(cacheKey);
  360. if (tableFilterItemList == null)
  361. {
  362. // 获取用户所属机构
  363. var orgIds = App.GetService<SysCacheService>().Get<List<long>>(CacheConst.KeyOrgIdList + userId);
  364. if (orgIds == null || orgIds.Count == 0) return;
  365. // 获取业务实体数据表
  366. var entityTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
  367. && u.BaseType == typeof(DataEntityBase));
  368. if (!entityTypes.Any()) return;
  369. var tableFilterItems = new List<TableFilterItem<object>>();
  370. foreach (var entityType in entityTypes)
  371. {
  372. // 排除非当前数据库实体
  373. var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
  374. if ((tAtt != null && (string)db.CurrentConnectionConfig.ConfigId != tAtt.configId.ToString()) ||
  375. (tAtt == null && (string)db.CurrentConnectionConfig.ConfigId != SqlSugarConst.ConfigId))
  376. continue;
  377. Expression<Func<DataEntityBase, bool>> dynamicExpression = u => orgIds.Contains((long)u.CreateOrgId);
  378. var tableFilterItem = new TableFilterItem<object>(entityType, dynamicExpression);
  379. tableFilterItems.Add(tableFilterItem);
  380. db.QueryFilter.Add(tableFilterItem);
  381. }
  382. db.DataCache.Add(cacheKey, tableFilterItems);
  383. }
  384. else
  385. {
  386. tableFilterItemList.ForEach(u =>
  387. {
  388. db.QueryFilter.Add(u);
  389. });
  390. }
  391. }
  392. /// <summary>
  393. /// 配置自定义过滤器
  394. /// </summary>
  395. private static void SetCustomEntityFilter(SqlSugarScopeProvider db)
  396. {
  397. // 排除超管过滤
  398. if (App.User?.FindFirst(ClaimConst.AccountType)?.Value == ((int)AccountTypeEnum.SuperAdmin).ToString())
  399. return;
  400. // 配置用户机构范围缓存
  401. var cacheKey = $"db:{db.CurrentConnectionConfig.ConfigId}:Custom";
  402. var tableFilterItemList = db.DataCache.Get<List<TableFilterItem<object>>>(cacheKey);
  403. if (tableFilterItemList == null)
  404. {
  405. // 获取自定义实体过滤器
  406. var entityFilterTypes = App.EffectiveTypes.Where(u => !u.IsInterface && !u.IsAbstract && u.IsClass
  407. && u.GetInterfaces().Any(i => i.HasImplementedRawGeneric(typeof(IEntityFilter))));
  408. if (!entityFilterTypes.Any()) return;
  409. var tableFilterItems = new List<TableFilterItem<object>>();
  410. foreach (var entityFilter in entityFilterTypes)
  411. {
  412. var instance = Activator.CreateInstance(entityFilter);
  413. var entityFilterMethod = entityFilter.GetMethod("AddEntityFilter");
  414. var entityFilters = ((IList)entityFilterMethod?.Invoke(instance, null))?.Cast<object>();
  415. if (entityFilters == null) continue;
  416. entityFilters.ForEach(u =>
  417. {
  418. var tableFilterItem = (TableFilterItem<object>)u;
  419. var entityType = tableFilterItem.GetType().GetProperty("type", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(tableFilterItem, null) as Type;
  420. // 排除非当前数据库实体
  421. var tAtt = entityType.GetCustomAttribute<TenantAttribute>();
  422. if ((tAtt != null && (string)db.CurrentConnectionConfig.ConfigId != tAtt.configId.ToString()) ||
  423. (tAtt == null && (string)db.CurrentConnectionConfig.ConfigId != SqlSugarConst.ConfigId))
  424. return;
  425. tableFilterItems.Add(tableFilterItem);
  426. db.QueryFilter.Add(tableFilterItem);
  427. });
  428. }
  429. db.DataCache.Add(cacheKey, tableFilterItems);
  430. }
  431. else
  432. {
  433. tableFilterItemList.ForEach(u =>
  434. {
  435. db.QueryFilter.Add(u);
  436. });
  437. }
  438. }
  439. }