I amusing Entity Framework 6 Code First, LINQ to Entities. I have a bunch of LINQ queries all doing good work against SQL Server. We have database guys looking at the performance of SQL server and identifying queries that are slow or consuming too many resources. They identify a bunch and give them back to us as SQL. Now we need easily and quickly match the "offending" LINQ queries and tweak them.
Is there a way to do this? How can this problem be solved?
I think this is a feature request unless there is something that can already do this that I don't know about.
Look into creating a DbCommandInterceptor (more info: https://docs.microsoft.com/en-us/ef/ef6/fundamentals/logging-and-interception) in which you can modify the DbCommand.CommandText - here you can add a comment to the SQL that uses StackTrace to grab the classes and method names on the stack so you can track down the code.
Something like:
var stack = new StackTrace(true);
command.CommandText += "\r\n\r\n/********************* \r\n * "
+ string.Join("\r\n * ",
(from f in stack.GetFrames().Skip(2)
let m = f.GetMethod()
select m?.DeclaringType.Name + "." + m?.Name + ":" + f.GetFileLineNumber())
) + "\r\n*********************/";
The suggestion from @duncansmart (or variations of this) is the only way I am aware of to do this.
Most helpful comment
Look into creating a
DbCommandInterceptor(more info: https://docs.microsoft.com/en-us/ef/ef6/fundamentals/logging-and-interception) in which you can modify the DbCommand.CommandText - here you can add a comment to the SQL that usesStackTraceto grab the classes and method names on the stack so you can track down the code.Something like: