EFCore.QueryGuard
1.0.0
dotnet add package EFCore.QueryGuard --version 1.0.0
NuGet\Install-Package EFCore.QueryGuard -Version 1.0.0
<PackageReference Include="EFCore.QueryGuard" Version="1.0.0" />
<PackageVersion Include="EFCore.QueryGuard" Version="1.0.0" />
<PackageReference Include="EFCore.QueryGuard" />
paket add EFCore.QueryGuard --version 1.0.0
#r "nuget: EFCore.QueryGuard, 1.0.0"
#:package EFCore.QueryGuard@1.0.0
#addin nuget:?package=EFCore.QueryGuard&version=1.0.0
#tool nuget:?package=EFCore.QueryGuard&version=1.0.0
EFCore.QueryGuard
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 | Versions 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. |
-
net8.0
- Microsoft.EntityFrameworkCore.Relational (>= 8.0.0)
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 |