Skip to content

EF Core Global Query Fix

DodaTech Updated 2026-06-24 2 min read

In this tutorial, you'll learn about EF Core Global Query Fix. We cover key concepts, practical examples, and best practices.

Your EF Core query returns fewer rows than expected, a query that used to work now silently filters data, or an admin page shows incomplete results. A global query filter is likely the cause — it is applied invisibly to every query.

The Problem

// A global query filter was configured:
modelBuilder.Entity<Order>().HasQueryFilter(o => !o.IsDeleted);

// An admin query:
var allOrders = await db.Orders.ToListAsync();
// Returns only non-deleted orders — even for admin!

Output: Customers see only active orders (correct). But the admin panel also hides deleted orders (incorrect).

The Fix

var allOrders = await db.Orders
    .IgnoreQueryFilters()
    .ToListAsync();

IgnoreQueryFilters() bypasses all global query filters for that query. Use it for admin operations, reporting, and data maintenance.

For conditional filters based on the caller:

public class AppDbContext : DbContext
{
    private readonly bool _isAdmin;

    public AppDbContext(DbContextOptions<AppDbContext> options, IHttpContextAccessor http)
        : base(options)
    {
        _isAdmin = http?.HttpContext?.User?.IsInRole("Admin") ?? false;
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Order>().HasQueryFilter(o =>
            _isAdmin || !o.IsDeleted);
    }
}

Now admin users see all records without needing IgnoreQueryFilters.

Prevention

  • Use IgnoreQueryFilters() for queries that should see all data.
  • Use context-level parameters for role-based filter behavior.
  • Log generated SQL to verify filters are applied as expected.
  • Document all global query filters in the DbContext.
  • Test queries with and without IgnoreQueryFilters().
  • Be aware that IgnoreQueryFilters applies to all filters on that entity.
  • Use IgnoreAutoInclude if navigation property auto-include is the issue.

Common Mistakes with core global query fix

  1. Using foldl instead of foldl' causing stack overflow on large lists
  2. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  3. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable

These mistakes appear frequently in real-world EF code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Can I ignore a specific filter but keep others?

No. IgnoreQueryFilters() disables all global query filters for the entity. If you need selective disabling, use a per-context flag parameter instead of multiple filters. Use a single combined filter with conditional logic.

Do navigation property queries also get the filter?

Yes. Global query filters apply to all queries for that entity, including navigation property loads via Include and lazy loading. Filtered entities loaded through navigation properties also have the filter applied.

Why is my query slower after adding a query filter?

The filter adds a WHERE clause. If the filtered column is not indexed, the database performs a full table scan. Add an index on the filtered column (e.g., IsDeleted, TenantId) to restore performance.

Learn more about EF Core query filters at DodaTech.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro