views:

210

answers:

2

In Link-2-SQL, I can use the DataContext.Log property to see the exact queries that are getting thrown to SQL Server.

Is there an equivalent to this in Entity Framework?

Thanks Daniel

+1  A: 

ObjectQuery.ToTraceString()

Craig Stuntz
Hmmmm, now, I have a table called Posts and one called Sites.If I take the result from "db.Posts", that has a ToTraceString() method. However, I want to look at the SQL that results from making a more complex query, in this case: from site in db.Sites orderby site.Order descending, site.Title select new SitePosts() { Site = site, Posts = site.Posts.OrderByDescending(p => p.PublicationTime) }How do I get the TraceString of that?
Daniel Magliola
Cast it as ObjectQuery.
Craig Stuntz
I'm getting a "Cannot convert type Models.Site to System.Data.Objects.ObjectQuery" cast error.I'm doing this:Site site = (the query I mentioned before); ObjectQuery oq = (ObjectQuery) site;What am I missing here?Thanks for your help!
Daniel Magliola
Cast the query, not the results. So if you have site = (query).First() change it to q = (query); sql = ((ObjectQuery)q).ToTraceString(); site = q.First();
Craig Stuntz
A: 

Since Entity Framework supports multiple backends (as opposed to Linq-to-SQL which is SQL Server only), you can't really get the actual SQL being sent to the backend server from EF.

In order to really see what's going on, I'd recommend firing up SQL Profiler on the SQL Server backend, and see what queries get sent its way.

See this article on Simple-Talk and possibly this video series on becoming a SQL Profiler master if you're not familiar with the SQL Profiler tool.

Marc

marc_s