百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
E

EntityFramework.Filters

> 编程语言
开源

Entity Framework 的过滤器实现

188 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

Entity Framework 的过滤器实现

EntityFramework.Filters

Filters implementation for Entity Framework, on NuGet as the EntityFramework.Filters package.

Filters allow you to define a parameterized filter at configuration time. At runtime, you turn on the filter and apply parameters, and every query for that entity will include the filter.

Configuration

The FilterInterceptor must be registered with Entity Framework, either through a DbConfiguration class:

csharp
public class ExampleConfiguration : DbConfiguration
{
    public ExampleConfiguration()
    {
        AddInterceptor(new FilterInterceptor());
    }
}

Or through the OnModelCreating method:

csharp
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    DbInterception.Add(new FilterInterceptor());
}

Examples

Filters are first defined, then configured. You define the filter against a single entity:

csharp
modelBuilder.Entity()
    .Filter("ActiveListings", c => c.Condition(
        listing => listing.Status == ListingStatus.Active));

Or against a set of entities that match a type (interface or base class):

csharp
modelBuilder.Conventions.Add(
    FilterConvention.Create("Agency", (e, agencyId) => e.AgencyId == agencyId);

Filters are then enabled and parameter values filled in on a DbContext basis:

csharp
dbContext.EnableFilter("ActiveListings");
dbContext.EnableFilter("Agency")
    .SetParameter("agencyId", _userContext.CurrentUser.AgencyId);

Filters are disabled by default, and you can disable them selectively after enabling:

csharp
dbContext.DisableFilter("ActiveListings");

The filter names must be unique, and filter parameter names are matched by the parameter name you supply to the filter definition's expression.

Common Usages

Filters are used to define a predicate that will be applied to every entity in a DbContext, without a developer needing to remember to include it for every query. Common applications include:

  • Security
  • Multi-tenancy
  • Logical data partitioning
  • Soft deletes
  • Active/inactive records

There are some limitations, however:

  • No access to context for complex joins
  • Collection properties not available

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C#

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言