当使用Entity Framework CTP 5“仅代码”时,如何获得原生SQL基础的LINQ查询? [英] How do I get the raw SQL underlying a LINQ query when using Entity Framework CTP 5 "code only"?

查看:148
本文介绍了当使用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.

这篇关于当使用Entity Framework CTP 5“仅代码”时,如何获得原生SQL基础的LINQ查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆