QueryGuard Report
ActionsAbout
Tags
(2)Find repeated EF Core queries and catch query problems in tests.
QueryGuard counts database queries and checks them against limits you set. For example, you can fail a test when the same query runs more than 5 times.
Repeated queries can be a sign of an N+1 problem. Some repeats are expected, so you can allow them with a reason.
Add this package to your ASP.NET Core test project:
dotnet add package QueryGuard.AspNetCore.TestingRun a request and check its queries:
using Microsoft.AspNetCore.Mvc.Testing;
using QueryGuard;
using QueryGuard.AspNetCore.Testing;
using QueryGuard.Testing;
using var factory = new WebApplicationFactory<Program>();
await using var guard = factory.TrackQueries<Program, AppDbContext>(
"GET /api/companies",
QueryGuardPolicy.Create("companies")
.WithMaxOccurrencesPerFingerprint(5));
var response = await guard.Client.GetAsync("/api/companies");
response.EnsureSuccessStatusCode();
QueryGuardAssert.Passes(await guard.CompleteAsync());Replace Program, AppDbContext, and the route with your app's values.
This test fails if a query with the same SQL pattern runs more than 5 times.
Works with xUnit, NUnit, MSTest, and TUnit. For services and background jobs, see the testing guide.
| Package | Use |
|---|---|
QueryGuard.AspNetCore.Testing |
Test ASP.NET Core requests |
QueryGuard.Testing |
Test services and background jobs |
QueryGuard.AspNetCore |
Track queries during HTTP requests |
QueryGuard.Reporting |
Export console, JSON, JUnit, Markdown, or SARIF reports |
QueryGuard.Cli |
Save query counts and check for changes in CI |
- .NET 8 and 10, with EF Core 8 and 10.
- Tested with SQLite, PostgreSQL, SQL Server, and MySQL. See provider details.
- EF Core only. Dapper and raw ADO.NET are not tracked.
- Parameter values and connection strings are not captured. SQL is redacted before reporting.
git clone https://github.com/Benziza/queryguard-dotnet.git
cd queryguard-dotnet
dotnet test samples/QueryGuard.SampleTestsThe sample shows a request that runs 51 queries and a fix that runs just one.
Resources
QueryGuard Report is not certified by GitHub. It is provided by a third-party and is governed by separate terms of service, privacy policy, and support documentation.