You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.Net Core、EF Core 3.0、Abp中默认过滤表的Enabled列?

Solution: Add a Global Query Filter for the Enabled Column in ABP + EF Core

Absolutely! You can set up an automatic global filter for your Enabled column so you don’t have to manually append .Where(w => w.Enabled) to every Repository.GetAll() call. Since you mentioned searching for "Global Filters" without luck, let me break down the exact steps tailored to ABP Framework and EF Core 3.0:

Step 1: (Optional) Create a Marker Interface (For Multiple Entities)

If you have multiple entities with an Enabled column, create a shared interface to standardize the property. This lets you apply the filter to all matching entities in one go:

public interface IEnabledEntity
{
    bool Enabled { get; set; }
}

Then update your entity to implement this interface:

public class YourEntity : FullAuditedEntity<Guid>, IEnabledEntity
{
    public bool Enabled { get; set; }
    // Add your other entity properties here
}

Step 2: Configure the Global Query Filter in Your DbContext

Open your project's DbContext class (usually named something like YourProjectDbContext) and override the OnModelCreating method. Here’s how to apply the filter:

Option A: Apply to All Entities Implementing IEnabledEntity

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder); // Don't forget to call the base ABP implementation

    // Loop through all entities and apply the filter to those with IEnabledEntity
    foreach (var entityType in modelBuilder.Model.GetEntityTypes())
    {
        if (typeof(IEnabledEntity).IsAssignableFrom(entityType.ClrType))
        {
            // Build the filter expression dynamically
            var parameter = Expression.Parameter(entityType.ClrType, "entity");
            var enabledProperty = Expression.Property(parameter, nameof(IEnabledEntity.Enabled));
            var filterLambda = Expression.Lambda(enabledProperty, parameter);

            modelBuilder.Entity(entityType.ClrType).HasQueryFilter(filterLambda);
        }
    }
}

Option B: Apply to a Single Entity Only

If you only need the filter for one specific entity, skip the interface and target it directly:

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    base.OnModelCreating(modelBuilder);

    modelBuilder.Entity<YourEntity>().HasQueryFilter(e => e.Enabled);
}

Step 3: Bypass the Filter When Needed

There will be cases where you need to retrieve disabled entities (e.g., admin dashboards). ABP’s repositories work seamlessly with EF Core’s IgnoreQueryFilters() method to disable the global filter temporarily:

// Get ALL entities, including disabled ones
var allEntities = await _yourEntityRepository.GetAll()
    .IgnoreQueryFilters()
    .ToListAsync();

How This Works

This leverages EF Core’s built-in query filtering feature, which automatically injects the .Where(e => e.Enabled) clause into every query for the targeted entities. Since ABP’s Repository.GetAll() returns an EF Core IQueryable, the filter is applied automatically without any extra code on your part.

内容的提问来源于stack exchange,提问作者James Southall

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.07 13:12:52