设置全局过滤,最终Sql语句有重复出现的过滤条件。 返回
SqlSugar
沟通中
11
313
wnf0000 发布于2周前
悬赏:0 飞吻

热忱回答(11)
-
fate sta VIP0
2周前提供完整代码
1、new sqlsugar部分加注入过滤器部分
2、查询代码。
0 回复 -
wnf0000 VIP0
2周前using Furion; using IChainStar.Infrastructure.Core.Consts; using IChainStar.Infrastructure.Core.Entities.Base; using IChainStar.Infrastructure.Cores.Services.Tenant; using SqlSugar; using System; using System.Collections.Generic; using System.Reflection; namespace IChainStar.Infrastructure.Core { /// <summary> /// 数据库上下文对象 /// </summary> public static class DbContext { /// <summary> /// SqlSugar 数据库实例 /// </summary> public static readonly SqlSugarScope Instance = new( // 读取 appsettings.json 中的 ConnectionConfigs 配置节点 App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs") , (db) => { db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices { EntityService = (c, p) => { /***低版本C#写法***/ // int? decimal?这种 isnullable=true 不支持string(下面.NET 7支持) if (p.IsPrimarykey == false && c.PropertyType.IsGenericType && c.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>)) { p.IsNullable = true; } /***高版C#写法***/ //支持string?和string if (p.IsPrimarykey == false && new NullabilityInfoContext() .Create(c).WriteState is NullabilityState.Nullable) { p.IsNullable = true; } } }; // 这里配置全局事件,比如拦截执行 SQL db.Aop.OnLogExecuting = (sql, pars) => { //Console.WriteLine(UtilMethods.GetNativeSql(sql,pars)); if (sql.StartsWith("SELECT")) { Console.ForegroundColor = ConsoleColor.Green; } if (sql.StartsWith("UPDATE") || sql.StartsWith("INSERT")) { Console.ForegroundColor = ConsoleColor.White; } if (sql.StartsWith("DELETE")) { Console.ForegroundColor = ConsoleColor.Blue; } Console.WriteLine(sql + "\r\n\r\n" + SqlProfiler.ParameterFormat(sql, pars)); }; //配置全局过滤器 db.QueryFilter.AddTableFilter<ISoftDeletedEntity>((it) => it.IsDeleted ==false); db.QueryFilter.AddTableFilter<ITenantEntity>((it) =>it.TenantId == TenantService.CurrentTenant); }); } }@fate sta:
0 回复 -
fate sta VIP0
2周前提供查询的代码。
0 回复 -
wnf0000 VIP0
2周前@fate sta:
0 回复 -
wnf0000 VIP0
2周前@fate sta:查询部分,我是根据前端传递的参数动态构建where表达式,不会附加这些过滤器的,我可以调试看看where表达式结构
0 回复 -
wnf0000 VIP0
2周前@fate sta:
0 回复 -
fate sta VIP0
2周前提供没有任何封装的代码,并且能重现
用 db.Queryable<xx>().ToPageList(1,,2) 测试,可能是你封装问题
0 回复 -
wnf0000 VIP0
2周前@fate sta:
0 回复 -
wnf0000 VIP0
2周前我找到原因了,query不能重复用,要clone一个
0 回复 -
wnf0000 VIP0
2周前@wnf0000:
0 回复 -
wnf0000 VIP0
2周前@fate sta:感谢大佬🙏
0 回复