How to show SQL command created by Entity Framework?

Updated version.

Sometimes you may need to look at the command, that’s created from your i.e. LINQ query and sent to database. Let’s say you have query like this:

var q = from m in e.master
           select m.t.Length;

You can cast the q into ObjectQuery and use the ToTraceString method to see the query:

Console.WriteLine(((ObjectQuery)q).ToTraceString());

This will show you the query, that’s sent to store you’re using (mainly relational database). Neat and easy.

4 thoughts on “How to show SQL command created by Entity Framework?

  1. cincura.net Post author

    Jarek Kowalski posted to MSDN Code Gallery wrappers for any ADO.NET Entity Framework provider with ability

  2. cincura.net Post author

    ToTraceString works only for queries. For insert etc. you have couple of options, for instance profiler, if your database engine supports this (via i.e. 3rd party product) or wrapping provider, as noted above or Entity Framework Profiler – http://www.efprof.com or …

Leave a Reply