过滤器
过滤器
Chloe 查询过滤器有两种,全局过滤器和单个上下文过滤器。
全局过滤器:
只能使用 fluent mapping 方式添加。
public class PersonMap : EntityTypeBuilder<Person>
{
public PersonMap()
{
this.MapTo("Person");
/* global filter */
this.HasQueryFilter(a => a.IsDeleted == false);
}
}
单个 DbContext 过滤器:
这种用法只会对单个 DbContext 有效,在创建 DbContext 时添加。
IDbContext dbContext = new MsSqlContext("Your connection string");
/* context filter,仅对当前 DbContext 对象有效 */
dbContext.HasQueryFilter<Person>(a => a.TenantId == 1);
查询时禁用过滤器:
IDbContext dbContext = new MsSqlContext("Your connection string");
IQuery<Person> q = dbContext.Query<Person>();
q = q.IgnoreAllFilters();