When using Entity Framework CTP 5 "code only," how can I retrieve the raw SQL underlying a LINQ query?

code-first ef4-code-only entity-framework profiling sql

Question

I've using Entity Framework CTP5 in "code only" mode. I'm running a LINQ query on a object that was return from the database, as the query is running really slowly. Is there any way in which I can get the SQL statement that is being generated from the query?

Topic currentTopic =
    (from x in Repository.Topics
     let isCurrent = (x.StoppedAt <= x.StartedAt || (x.StartedAt >= currentTopicsStartedAtOrAfter))
     where x.Meeting.Manager.User.Id == user.Id && isCurrent
     orderby x.StartedAt descending
     select x).FirstOrDefault();

The "Repository" property is a descendent of DbContext.

It's a little complicated, as EF can't use my helper methods on the objects, so I'm specifying the logic directly in the query.

So, is there any way I can dump the SQL that will be produced by that LINQ query (e.g. to my log4net repository)?

1
15
3/1/2011 11:19:25 AM

Accepted Answer

I'd either use SQL Trace to grab the query running on the server directly, or use the Event Tracing for Windows (SQL Profiling) feature out of ANTS Performance Profiler.

3
3/1/2011 11:30:30 AM

Popular Answer

You can try using Entity Framework tracing provider as described here (but it is old post for CTP3).

Your other choices are:

In common EF you can also use ToTraceString as @Andy suggested but DbQuery in CodeFirst doesn't have this method (or I didn't find it).

Edit:

So DbQuery doesn't have ToTraceString because it is directly implemented as ToString.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow