Skip to content

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.

  • .NET 8.0+
  • Entity Framework Core >= 8.0
Terminal window
dotnet add package QueryDoctor.SqlCommenter.EFCore

Chain .AddSqlCommenter() onto your database configuration:

options.UseSqlServer(connectionString).AddSqlCommenter();

Every query executed through EF Core now includes a SQL comment with:

TagIncluded by defaultDescription
actionYesController action or method name
controllerYesController name (without the Controller suffix)
db_driverYesIdentifies EF Core as the driver
fileYesSource file, line, and column via stack inspection
frameworkYesEF Core version (e.g. efcore:8.0)
methodYesHTTP 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'*/
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)
});

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].

  • 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