EFCore.QueryGuard 1.0.0

dotnet add package EFCore.QueryGuard --version 1.0.0
                    
NuGet\Install-Package EFCore.QueryGuard -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EFCore.QueryGuard" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="EFCore.QueryGuard" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="EFCore.QueryGuard" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add EFCore.QueryGuard --version 1.0.0
                    
#r "nuget: EFCore.QueryGuard, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package EFCore.QueryGuard@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=EFCore.QueryGuard&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=EFCore.QueryGuard&version=1.0.0
                    
Install as a Cake Tool

EFCore.QueryGuard

NuGet Version NuGet Downloads License: MIT Build

A lightweight EF Core interceptor that detects common query problems at runtime — before they hit production.

  • N+1 detection — flags when the same query pattern repeats above a configurable threshold
  • Slow query alerts — warns when individual queries exceed a time threshold
  • Excessive query count — catches scopes with too many queries (e.g., loops that should be batched)

Installation

dotnet add package EFCore.QueryGuard

For ASP.NET Core middleware integration:

dotnet add package EFCore.QueryGuard.AspNetCore

Quick Start

In your DbContext

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
    optionsBuilder
        .UseSqlServer(connectionString)
        .UseQueryGuard(options =>
        {
            options.SlowQueryThresholdMs = 300;
            options.DetectNPlusOne = true;
            options.NPlusOneThreshold = 3;
        });
}

In ASP.NET Core (Program.cs)

// Register the interceptor and options
builder.Services.AddQueryGuard(options =>
{
    options.SlowQueryThresholdMs = 200;
    options.DetectNPlusOne = true;
});

// Register your DbContext, wiring in the interceptor
builder.Services.AddDbContext<AppDbContext>((sp, options) =>
{
    var interceptor = sp.GetRequiredService<QueryGuardInterceptor>();
    options.UseSqlServer(connectionString)
           .AddInterceptors(interceptor);
});

// Add middleware — wraps each HTTP request in a QueryGuard scope
app.UseQueryGuard();

Each HTTP request is automatically wrapped in a scope. Violations are logged as warnings and surfaced via the X-QueryGuard-Violations: {count} response header.


Configuration

All options are set via QueryGuardOptions:

Property Default Description
SlowQueryThresholdMs 500 Warn if a query takes longer than this many milliseconds
MaxQueriesPerScope 10 Warn if total queries within a scope exceed this count
DetectNPlusOne true Enable N+1 query pattern detection
NPlusOneThreshold 3 Number of times a query pattern must repeat before flagging as N+1
ThrowOnViolation false Throw QueryGuardException instead of just logging
OnViolation null Custom Action<QueryViolation> callback invoked on each violation

What Violations Look Like

QueryGuard logs violations at Warning level via ILogger:

warn: EFCore.QueryGuard.QueryGuardInterceptor
      [QueryGuard:NPlusOne] N+1 query detected: same query pattern executed 3 times (threshold: 3): SELECT "p"."Id", "p"."Name" FROM "Products" AS "p" WHERE "p"."CategoryId" = @__id_0

warn: EFCore.QueryGuard.QueryGuardInterceptor
      [QueryGuard:SlowQuery] Slow query detected (612ms > threshold 500ms): SELECT "p"."Id", "p"."Name", "p"."Price" FROM "Products" AS "p"

warn: EFCore.QueryGuard.QueryGuardInterceptor
      [QueryGuard:ExcessiveQueryCount] Excessive query count: 11 queries executed in scope (max: 10)

When using the ASP.NET Core middleware, violations are also reflected in the response header:

X-QueryGuard-Violations: 3

License

MIT — see LICENSE.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on EFCore.QueryGuard:

Package Downloads
EFCore.QueryGuard.AspNetCore

ASP.NET Core middleware integration for EFCore.QueryGuard — automatically scopes N+1, slow query, and excessive query count detection per HTTP request.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 51 6/1/2026