Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to "tag" Entity Framework Queries [duplicate]

I am helping improve the performance of a complex application with hundreds of Entity Framework queries scattered throughout the code base. One problem we have is that when we run the SQL Profiler and capture the Entity Framework queries it is very time consuming to find the code that actually resulted in the sql statement that comes through to the profiler. I am wondering if there is some way we could "tag" each entity framework statement with a unique identifier that would come through in the sql statement. Something along the lines of adding a comment to the actual sql statement that entity framework generates for each statement. We could then see the comment in the profiler trace, and search the code for the tag. Any ideas on how we could go about doing this? I'm guessing there must be some way we could intercept the sql that EF generates and add our comment to it before it goes out to the db server.

like image 516
Toe Spam Avatar asked Nov 20 '15 19:11

Toe Spam


1 Answers

I'd personally use a code profiler rather than SQL profiler (I prefer JetBrain's dotTrace). Entity Framework will sometimes generate several queries depending upon the operation it has to perform, so it's not going to be trivial to tag each query you see to a particular operation because it won't necessarily find it's way into each query.

In my experience, EF performance problems are usually due to an operation put on a linq query in the wrong place that performs a full table load and hydrates a ton of objects. The code profiler will also allow you to focus exactly where in the code to fix these problems rather than hunting down the operations that are creating the offending SQL statements.

like image 155
MutantNinjaCodeMonkey Avatar answered Nov 03 '22 17:11

MutantNinjaCodeMonkey