In EF 4.1 DbContext how to trace generated SQL
The easiest way with DbContext
and DbSet<T>
is just to use ToString()
on the IQueryable
you have built. For example:
var query = context.Blogs.Include(b => b.Posts) .Where(b => b.Title == "AnyTitle");string sql = query.ToString();
sql
contains the SQL command which will be issued to the DB when the query gets executed.
The MVC-Mini-Profiler is a pwerful tool, not ony trace generated sql, but also profiling tool.
Using mvc-mini-profiler database profiling with Entity Framework Code First
I use the SQL Server profiler tool to see exactly what SQL has been created. There is also http://efprof.com/ but it has quite a high price.