What is query tags in EF Core?

It’s introduced in EF Core 2.2. This feature helps correlate LINQ queries in code with generated SQL queries captured in logs. You annotate a LINQ query using the new _TagWith()_ method.

var publishedBlogPosts = dbContext.BlogPosts
    .Where(b => b.PublishedAt != null)
    .TagWith("Getting published blog posts")
    .ToList();

When EF generates the SQL with TagWith method, it also includes the tag as a comment in the query; as a result, debugging and profiling queries might be easier.

-- Getting published blog posts

SELECT [b].[BlogPostId], [b].[Content], [b].[PublishedAt], [b].[Title]
      FROM [BlogPosts] AS [b]
      WHERE [b].[PublishedAt] IS NOT NULL

#c-sharp-programming #dotnet-core #entity-framework-core #dotnet #query #c++

Practical Query Tagging in EF Core
1.95 GEEK