Skip to content
Home » Blog » Use Query Tags with EF Core

Use Query Tags with EF Core

Today, let’s spotlight a feature of EF Core – Query Tags! 🏷️

EF Core

Query Tags allow us to correlate application code and SQL queries generated, making it easier to connect the dots when debugging or profiling.

When you attach a unique Tag to your LINQ queries, you essentially provide a way to track the generated SQL query.

One great usage is to support a Correlation Identifier (CId). The CId could, in practice, be initialised from different contexts, HTTP requests, Message Broker, etc. It allows tracking the process flow requiring integration with different tiers — Web Server, Database, or any Message Broker by example. 🎯

You might think it’s a complex feature, but it’s simple. Just a matter of adding .TagWith("YourTag") to your LINQ statement.

C#

var cid = Guid.NewGuid();

var blogs = _context.Blogs
					.TagWith($"CorrelationId={cid}")
					.ToList();

SQL

-- CorrelationId=7b79f0a4-e833-4e06-bb1a-01270ed66bad

SELECT *
FROM [Blogs]

Finally, EF Core – Query Tags feature can really be helpful in investigating issues or performance bottlenecks.

Happy Coding! 🚀

References