I wonder how to trace generated SQL like DataContext in LinqToSql.
I also read articles about the solution of EFProviderWrapper on Jaroslaw Kowalski's blog, but it is based on ObjectContext, does not work for DbContext.
Anyone know how to do this in DbContext?
Thank you.
To view the SQL that will be generated, simply call ToTraceString() . You can add it into your watch window and set a breakpoint to see what the query would be at any given point for any LINQ query. You can attach a tracer to your SQL server of choice, which will show you the final query in all its gory detail.
var q = from img in context. Images ... select img; string sql = q. ToString(); sql will contain the sql select query.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With