Tag your Entity Framework 6 LINQ queries, and see these tags in the SQL commands sent to the database. This will allow you to easily identify queries while using tools like SQL Profiler or Azure's performance and diagnostic features.
(Yes, it is a simple implementation of Entity Framework Core's query tags feature.)
- Install the NuGet package in your project.
PM> install-package EF6.TagWith
- Add the query interceptor. The easiest way is just to add the following code somewhere in your application startup code:
DbInterception.Add(new QueryTaggerInterceptor(new SqlServerTagger()));- Tag your queries this way:
var query = context.Friends
.OrderByDescending(friend => friend.Age)
.Take(10)
.Select(friend => new { FriendName = friend.Name, friend.Age, CountryName = friend.Country.Name })
.TagWith("Get top 10 older friends with country");- The query sent to the database will be as follows:
-- Get top 10 older friends with country
SELECT TOP(@__p_0) [friend].[Name] AS [FriendName], [friend].[Age],
[friend.Country].[Name] AS [CountryName]
FROM [Friends] AS [friend]
LEFT JOIN [Countries] AS [friend.Country] ON [friend].[CountryId] = [friend.Country].[Id]
ORDER BY [friend].[Age] DESCThe TagWith() extension method adds a special predicate to the query, so it can be easily identified in the final SQL.
Later on, just before sending the SQL command to the database, we use EF 6 interceptors to extract this predicate and insert the tag as a comment, just using a bit of string wizardry.
- The component only supports SQL Server, but can be easily adapted to support another providers just creating a new implementation of
ISqlTaggerand using it in the interceptor configuration.
Feel free to send issues, comments or pull requests. The following users have contributed to this project so far:
Thank you!