SqlSugar小技巧 - 过滤器

SqlSugar 是一款 老牌 .NET 开源ORM框架,由果糖大数据科技团队维护和更新 ,开箱即用最易上手的ORM框架 。生态圈丰富,目前开源生态仅次于微软的EF Core。文章主要记录一些使用SqlSugar过滤器的小技巧

1 配置表过滤器

1
2
3
4
5
6
7
8
//配置表过滤器
db.QueryFilter.Add(new TableFilterItem<StudentInfo>(it => it.Name.Contains("名称")));

//查询操作自带过滤器效果 操作表时会带 where name like '%名称%' 的条件.
List<OdinLog> resultlist = db.Queryable<OdinLog>().ToList();

//删除操作需要使用 EnableQueryFilter() 添加过滤器
db.Deleteable<OdinLog>().EnableQueryFilter().Where(it => it.Id == 15).ExecuteCommand();

2 逻辑删除

1
2
3
4
5
6
7
8
9
10
11
//逻辑删除 请升级到5.0.4.9+

//实体属性有isdelete或者isdeleted
//假删除 软删除
db.Deleteable<StudentInfo>().In(20).IsLogic().ExecuteCommand();

//指定属性逻辑删除的特殊字段名
db.Deleteable<StudentInfo>().In(21).IsLogic().ExecuteCommand("IsEnable");

//指定属性逻辑删除的特殊字段名和值并且修改时间
db.Deleteable<StudentInfo>().In(22).IsLogic().ExecuteCommand("IsEnable", 1, "CreateTime");