SQLCommenter for EF Core
QueryDoctor.SqlCommenter.EFCore is an EF Core interceptor that appends SQLCommenter-formatted comments to every SQL query. Query Doctor reads these comments to connect queries back to the controller, action, and source file that produced them.
Prerequisites
Section titled “Prerequisites”- .NET 8.0+
- Entity Framework Core >= 8.0
Installation
Section titled “Installation”dotnet add package QueryDoctor.SqlCommenter.EFCoreBasic setup
Section titled “Basic setup”Chain .AddSqlCommenter() onto your database configuration:
options.UseSqlServer(connectionString).AddSqlCommenter();Every query executed through EF Core now includes a SQL comment with:
| Tag | Included by default | Description |
|---|---|---|
action | Yes | Controller action or method name |
controller | Yes | Controller name (without the Controller suffix) |
db_driver | Yes | Identifies EF Core as the driver |
file | Yes | Source file, line, and column via stack inspection |
framework | Yes | EF Core version (e.g. efcore:8.0) |
method | Yes | HTTP request method (GET, POST, etc.) |
Example annotated query:
SELECT * FROM "Users" WHERE "Id" = @p0/*action='GetUser',controller='Users',db_driver='efcore', file='Controllers%2FUsersController.cs%3A42%3A1', framework='efcore%3A8.0',method='GET'*/Configuration
Section titled “Configuration”options.UseSqlServer(connectionString).AddSqlCommenter(o =>{ o.Enabled = true; // Enable/disable (default: true) o.EnableStackInspection = true; // Auto-detect caller info (default: true) o.MaxStackDepth = 30; // Max stack frames to inspect (default: 30) o.IncludeFrameworkVersion = true; // Include EF Core version (default: true)});Explicit context
Section titled “Explicit context”For precise control over the tags, use QueryTaggingContext.SetContext() instead of (or alongside) automatic stack inspection:
using (QueryTaggingContext.SetContext(action: "GetUser", controller: "Users")){ var user = await context.Users.FindAsync(id);}Explicit context always takes priority over stack inspection. Caller file path, line number, and member name are captured automatically via [CallerFilePath], [CallerLineNumber], and [CallerMemberName].
Spec compliance
Section titled “Spec compliance”- Keys are sorted lexicographically
- Values are URL-encoded and wrapped in single quotes
- SQL that already contains comments (
/*or*/) is not modified - Errors during tagging never fail the query