Skip to content
Skills Library
Programming · Building & coding

apply-data-filters

/apply-data-filters

Work with ABP data filters — the built-in ISoftDelete and IMultiTenant filters, toggling them via IDataFilter scopes, changing default states, and defining custom global query filters for EF Core or MongoDB. USE FOR: reading/writing soft-deleted or cross-tenant data with IDataFilter.Disable/Enable, checking IsEnabled, HardDeleteAsync, changing AbpDataFilterOptions defaults, defining a custom marker-interface filter, wiring it in EF Core (ShouldFilterEntity/CreateFilterExpression/HasAbpQueryFilter) or MongoDB (MongoDbRepositoryFilterer/FilterQueryable). DO NOT USE FOR: switching the active tena

Repository
abp-skills ↗
Licence
MIT
Last repository change
05.10.2026

Assessment and security

100/ 100Class: good
Format25 / 25
Frontmatter with name and description, valid name, description up to 1,024 characters, name matching the folder.
Discoverability25 / 25
Description explains when to use the skill, has a reasonable length and uses third person.
Content35 / 35
At least 150 words and at most 500 lines, headings, step lists, examples or code.
Hygiene15 / 15
No placeholders such as TODO, no Windows paths, with a licence.
Riskno signals detected in the scan
Files in this package
1
Scripts
0
HTML comments
0
URLs in SKILL.md
0
Deprecation

No signals detected. Repository changed on 05.10.2026, without references to retired models or APIs.

Licence

MIT, open: use and modify while retaining author attribution.

What it does and when to use it

Work with ABP data filters — the built-in ISoftDelete and IMultiTenant filters, toggling them via IDataFilter scopes, changing default states, and defining custom global query filters for EF Core or MongoDB. USE FOR: reading/writing soft-deleted or cross-tenant data with IDataFilter.Disable/Enable, checking IsEnabled, HardDeleteAsync, changing AbpDataFilterOptions defaults, defining a custom marker-interface filter, wiring it in EF Core (ShouldFilterEntity/CreateFilterExpression/HasAbpQueryFilter) or MongoDB (MongoDbRepositoryFilterer/FilterQueryable). DO NOT USE FOR: switching the active tena

Original author description

Work with ABP data filters — the built-in ISoftDelete and IMultiTenant filters, toggling them via IDataFilter scopes, changing default states, and defining custom global query filters for EF Core or MongoDB. USE FOR: reading/writing soft-deleted or cross-tenant data with IDataFilter.Disable/Enable, checking IsEnabled, HardDeleteAsync, changing AbpDataFilterOptions defaults, defining a custom marker-interface filter, wiring it in EF Core (ShouldFilterEntity/CreateFilterExpression/HasAbpQueryFilter) or MongoDB (MongoDbRepositoryFilterer/FilterQueryable). DO NOT USE FOR: switching the active tena

How it works

  1. When to Use
  2. When Not to Use
  3. How it works
  4. Validation
  5. Common Pitfalls
Original SKILL.md headings · 211 lines, 1,189 words
SKILL.md preview
---
name: apply-data-filters
description: >
  Work with ABP data filters — the built-in ISoftDelete and IMultiTenant filters, toggling them via IDataFilter scopes, changing default states, and defining custom global query filters for EF Core or MongoDB.
  USE FOR: reading/writing soft-deleted or cross-tenant data with IDataFilter.Disable/Enable, checking IsEnabled, HardDeleteAsync, changing AbpDataFilterOptions defaults, defining a custom marker-interface filter, wiring it in EF Core (ShouldFilterEntity/CreateFilterExpression/HasAbpQueryFilter) or MongoDB (MongoDbRepositoryFilterer/FilterQueryable).
  DO NOT USE FOR: switching the active tenant context via ICurrentTenant.Change (that is the multi-tenancy concern); defining a DbContext/repositories or EF Core migrations (use ef-core-integration); MongoDB DbContext/repository setup (use mongodb-integration); transaction/UOW boundaries (use manage-units-of-work); optimistic concurrency (use handle-optimistic-concurrency).
license: MIT
---

# Apply ABP Data Filters

`Volo.Abp.Data` provides **data filters** that automatically filter rows when querying the database. Filters are keyed by a marker interface implemented on the entity (`ISoftDelete`, `IMultiTenant`, or your own).

## When to Use

- Reading soft-deleted or cross-tenant rows by disabling `ISoftDelete` / `IMultiTenant` in an `IDataFilter` scope.
- Checking a filter's current state with `IsEnabled<TFilter>()`, or physically removing a soft-delete row with `HardDeleteAsync`.
- Changing whether a filter is enabled by default via `AbpDataFilterOptions`.
- Defining a custom marker-interface filter and wiring it for EF Core or MongoDB.

## When Not to Use

- **Switching the active tenant context** (`ICurrentTenant.Change`) — that is the multi-tenancy concern, **not** a data-filter one.
- **Defining a DbContext, repositories, or EF Core migrations** — use the **ef-core-integration** skill.
- **MongoDB DbContext / repository setup** — use **mongodb-integration**.
- **Transaction / Unit of Work boundaries** — use **manage-units-of-work**.
- **Optimistic concurrency** — use **handle-optimistic-concurrency**.

## How it works

### Built-in filters

#### `ISoftDelete`

Marks an entity as deleted instead of physically removing it. Implement `ISoftDelete` to add a `bool IsDeleted` property:

```csharp
public class Book : AggregateRoot<Guid>, ISoftDelete
{
    public string Name { get; set; }
    public bool IsDeleted { get; set; } // defined by ISoftDelete
}
```

When you delete via a repository, ABP sets `IsDeleted = true` and blocks the real delete, and it **automatically excludes** soft-deleted rows from queries. The filter is **enabled by default** — you cannot read deleted rows unless you explicitly disable it. Use `HardDeleteAsync` on the repository to physically remove a soft-delete entity.

#### `IMultiTenant`

Isolates data per tenant. Implement `IMultiTenant` to add a nullable `TenantId`:

```csharp
public class Book : AggregateRoot<Guid>, ISoftDelete, IMultiTenant
{
    public string Name { get; set; }
    public bool IsDeleted { get; set; }   // ISoftDelete
    public Guid? TenantId { get; set; }    // IMultiTenant
}
```

ABP automatically filters queries to the **current tenant** based on `TenantId`. (To switch tenant context you use `ICurrentTenant.Change`, which is the multi-tenancy concern, not the data-filter one.)

### Enabling / disabling filters: `IDataFilter`

Inject `IDataFilter` and toggle a filter inside a `using` block. The `IDisposable` restores the previous state when the block ends:

```csharp
public class MyBookService : ITransientDependency
{
    private readonly IDataFilter _dataFilter;
    private readonly IRepository<Book, Guid> _bookRepository;

    public MyBookService(IDataFilter dataFilter, IRepository<Book, Guid> bookRepository)
    {
        _dataFilter = dataFilter;
        _bookRepository = bookRepository;
    }

    public async Task<List<Book>> GetAllIncludingDeletedAsync()
    {
        using (_dataFilter.Disable<ISoftDelete>())
        {
            return await _bookRepository.GetListAsync(); // includes soft-deleted rows
        }
    }
}
```

`IDataFilter` members:

- `IDisposable Disable<TFilter>()` — disable a filter for the scope.
- `IDisposable Enable<TFilter>()` — enable a filter for the scope.
- `bool IsEnabled<TFilter>()` — check current state.

`Enable` and `Disable` **nest**, so you can define inner scopes and each restores the outer state on dispose. **Always** call them inside a `using` block so the state resets even on exception.

#### Generic `IDataFilter<TFilter>`

For a single filter you can inject the strongly-typed version and drop the type argument:

```csharp
private readonly IDataFilter<ISoftDelete> _softDeleteFilter;
// ...
using (_softDeleteFilter.Disable())
{
    return await _bookRepository.GetListAsync();
}
```

It exposes `Enable()`, `Disable()`, and `IsEnabled` — all bound to `TFilter`.

### Changing default filter state

Use `AbpDataFilterOptions` to change whether a filter is enabled by default. Example — disable soft delete globally (queries then include deleted rows unless you enable it):

```csharp
Configure<AbpDataFilterOptions>(options =>
{
    options.DefaultStates[typeof(ISoftDelete)] = new DataFilterState(isEnabled: false);
});
```

Be careful changing defaults for built-in filters, especially when using pre-built modules that assume soft delete is on. Changing defaults for **your own** filters is safe.

### Defining a custom filter

Start with a marker interface and implement it on entities:

```csharp
public interface IIsActive
{
    bool IsActive { get; }
}

public class Book : AggregateRoot<Guid>, IIsActive
{
    public string Name { get; set; }
    public bool IsActive { get; set; }
}
```

How the filter is actually **applied to queries depends on the database provider** — you implement it once per provider you use.

#### EF Core

ABP maps filters onto EF Core's **global query filters**, so they work even when you use `DbContext` directly. Override `ShouldFilterEntity` and `CreateFilterExpression` in your `DbContext`:

```csharp
protected bool IsActiveFilterEnabled => DataFilter?.IsEnabled<IIsActive>() ?? false;

protected override bool ShouldFilterEntity<TEntity>(IMutableEntityType entityType)
{
    if (typeof(IIsActive).IsAssignableFrom(typeof(TEntity)))
    {
        return true;
    }
    return base.ShouldFilterEntity<TEntity>(entityType);
}

protected override Expression<Func<TEntity, bool>>? CreateFilterExpression<TEntity>(
    ModelBuilder modelBuilder,
    EntityTypeBuilder<TEntity> entityTypeBuilder)
{
    var expression = base.CreateFilterExpression<TEntity>(modelBuilder, entityTypeBuilder);
    if (typeof(IIsActive).IsAssignableFrom(typeof(TEntity)))
    {
        Expression<Func<TEntity, bool>> isActiveFilter =
            e => !IsActiveFilterEnabled || EF.Property<bool>(e, "IsActive");
        expression = expression == null
            ? isActiveFilter
            : QueryFilterExpressionHelper.CombineExpressions(expression, isActiveFilter);
    }
    return expression;
}
```

Note the guard `!IsActiveFilterEnabled || ...` — the filter is applied only while it's enabled, which is what makes `Disable<IIsActive>()` work. For a single entity you can also chain onto ABP's built-in filters with `b.HasAbpQueryFilter(e => e.Name.StartsWith("abp"))` inside `OnModelCreating`.

#### MongoDB

For MongoDB, filtering goes through `IMongoDbRepositoryFilterer` and only works when you use the repositories. To add a filter for one entity, derive `MongoDbRepositoryFilterer<Book, Guid>`, override `FilterQueryable`, and expose it:

```csharp
[ExposeServices(typeof(IMongoDbRepositoryFilterer<Book, Guid>))]
public class BookMongoDbRepositoryFilterer
    : MongoDbRepositoryFilterer<Book, Guid>, ITransientDependency
{
    public BookMongoDbRepositoryFilterer(IDataFilter dataFilter, ICurrentTenant currentTenant)
        : base(dataFilter, currentTenant) { }

    public override TQueryable FilterQueryable<TQueryable>(TQueryable query)
    {
        if (DataFilter.IsEnabled<IIsActive>())
        {
            return (TQueryable)query.Where(x => x.IsActive);
        }
        return base.FilterQueryable(query);
    }
}
```

The query filter is `FilterQueryable` — the repository's `IQueryable`/`GetListAsync` pipeline runs each registered `IMongoDbRepositoryFilterer<TEntity>` (and, for keyed repositories, `IMongoDbRepositoryFilterer<TEntity, TKey>`) through it. So register your subclass for the matching filterer interface; if the entity is used through a keyed repository (`IRepository<Book, Guid>`), expose `IMongoDbRepositoryFilterer<Book, Guid>` and derive `MongoDbRepositoryFilterer<Book, Guid>` (as above) so the keyed pipeline picks it up.

`AddGlobalFiltersAsync` is a **different** hook: it builds native MongoDB `FilterDefinition<TEntity>`s used by the entity/id operations (`CreateEntityFilterAsync` / `CreateEntitiesFilterAsync` for get-by-id, delete, concurrency), **not** the queryable pipeline. Don't override it to add a global query filter — override `FilterQueryable` for that. The key point is the same as EF Core: honor `DataFilter.IsEnabled<...>()` so `Disable`/`Enable` scopes take effect.

## Validation

- By default a repository query excludes soft-deleted (`IsDeleted = true`) rows and rows outside the current tenant; the same query inside `_dataFilter.Disable<ISoftDelete>()` returns the deleted rows too.
- `IDataFilter.IsEnabled<TFilter>()` reflects the current scope's state and reverts when the `using` block ends (even on exception).
- For a custom EF Core filter, confirm the generated SQL includes the guard so `Disable<IIsActive>()` returns the otherwise-filtered rows.
- For a custom MongoDB filter, confirm the filtered rows appear only when going through the repository (the filterer runs there), and honor `DataFilter.IsEnabled<...>()`.

## Common Pitfalls

- **Toggling a filter without a `using` block** — the state won't reset on exception (or at all). Always scope `Enable`/`Disable` in `using`.
- **Changing `AbpDataFilterOptions` defaults for built-in filters** — pre-built modules assume soft delete is on; changing that globally can break them. Changing defaults for your **own** filters is safe.
- **Custom filter with no enabled-guard** — omit `!IsActiveFilterEnabled || ...` (EF Core) or the `IsEnabled<...>()` check (MongoDB) and `Disable`/`Enable` scopes silently do nothing.
- **MongoDB filters bypassed via the raw collection** — the filterer only runs through the repositories; native driver access is unfiltered.
- **Confusing tenant filtering with tenant switching** — `IMultiTenant` filters to the current tenant; changing the active tenant is `ICurrentTenant.Change`, a separate multi-tenancy concern.

Versions

The database has no other skills named apply-data-filters.

Database snapshot: 13.11.2026 · package version 3efe6a1e2d84 (SHA-256 digest of the entire package)

Downloads in the last 30 days: 0 · ranking

: harmful content, incorrect descriptions or an author’s removal request.

Similar skills

  • 100abp-multitenancy
    ABP Framework v10.x (10.4–10.6) multi-tenancy: tenant resolver, ICurrentTenant, IMultiTenant, database isolation, tenant-based data filtering. Use when working with SaaS, multi-tenancy, or tenant management in ABP.
  • 100abp-multitenancy
    ABP Framework v10.x (10.4–10.6) multi-tenancy: tenant resolver, ICurrentTenant, IMultiTenant, database isolation, tenant-based data filtering. Use when working with SaaS, multi-tenancy, or tenant management in ABP.
  • 100configure-multi-tenancy
    Configure ABP multi-tenancy with explicit host/tenant boundaries, tenant-aware entities and request resolution, and cross-tenant background execution. USE FOR: implementing IMultiTenant; using ICurrentTenant.Change safely; distinguishing host rows from all-tenant queries;…
  • 100mongodb-integration
    Integrate MongoDB into an ABP application: AbpMongoDbContext, collection mapping, AddMongoDbContext registration, and custom Mongo repositories. USE FOR: defining an AbpMongoDbContext, mapping collections with [MongoCollection]/CreateModel, registering via AddMongoDbContext,…
  • 90database-mongodb
    Apply MongoDB data-modeling, indexing, and query rules from access patterns. Use when designing schemas, choosing embed vs reference, or tuning MongoDB query behavior.
  • 100orchardcore-users-admin-list-filters
    Skill for adding custom filters to the Orchard Core users admin list (Security → Users). Covers IUsersAdminListFilterProvider named and default terms with OneCondition/ManyCondition against YesSql indexes over User, changing the bare-text term and exact match with…

Feedback and reports

Your message goes to Bartosz Sokoliński. An email address is needed only for a reply. See the privacy policy.