2

On many sites/blogs I noticed that people, who want to explain linq mechanism, write Linq Queries and provide SQL translation for that. How can I get the translation for my own queries? I'm working with EF .Net 4.0 in VS2010. Is there a place (property or even some third-party tool) where I can see what they're being translated into?

2
  • 1
    Not a direct answer, but you should look at EFProf. Yes it's a commercial tool, but man is it awesome. (There are free ways to do it too, such as SQL Profiler)
    – vcsjones
    Dec 19, 2011 at 15:20
  • That is exactly what I was looking for! Thank you, it works perfectly and I'm satisfied with 30-days trial :) Dec 19, 2011 at 16:57

3 Answers 3

5

You can use the ObjectQuery.ToTraceString method:

var query = ...
string sql = ((System.Data.Objects.ObjectQuery)query).ToTraceString();

However, some shortcomings of this approach is that it will only show queries, not updates/inserts/deletes. Also, it won't reveal the parameters used.

If you need as much detail as possible from the query then use the SQL Server Profiler as Neil suggested.

Julie Lerman has a good article with different options on MSDN: Profiling Database Activity in the Entity Framework. She mentions:

1
  • Yeah, it's really limited and does not allow to translate a lot of queries that I would like to, thanks however :) Dec 19, 2011 at 16:55
4

The simplest method is to use SQL Profiler. I have found this a real god-send when it comes to debugging.

1
  • Thanks, that is what I was looking for but I have only Express edition of MSSQL, so I would have to play with some more-advanced edition installer's scripts to get the profiler, so I just pass now as I'm in hurry. Dec 19, 2011 at 16:56
0

You can use LinqPad, you can query your entity model and view the generated SQL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.