Alex Rivera | Logout

How do I get the raw SQL underlying a LINQ query when using Entity Framework CTP 5 "code only"?

Asked 2011-03-01T11:19:25.007
16

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)?

Edit
Report

1 Answer

1

The extension method ToTraceString() might be what you're looking for:

http://msdn.microsoft.com/en-us/library/system.data.objects.objectquery.totracestring.aspx

answered 2011-03-01T12:11:20.550

Your Answer