当使用Entity Framework CTP 5“仅代码”时,如何获得原生SQL基础的LINQ查询?

问题描述:

我在仅代码模式中使用了实体框架CTP5。对于从数据库返回的对象,我正在运行一个LINQ查询,因为查询运行速度非常慢。有什么办法可以得到从查询生成的SQL语句?

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();

Repository属性是DbContext的后代。

The "Repository" property is a descendent of DbContext.

这有点复杂,因为EF不能在对象上使用我的帮助方法,所以我直接在查询中指定逻辑。

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.

那么有没有什么办法可以转储由LINQ查询产生的SQL(例如到我的log4net库)?

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

p>我将使用SQL Trace直接抓取在服务器上运行的查询,或者使用 ANTS性能分析器

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.