我在“仅代码”模式下使用 Entity Framework CTP5。我正在对从数据库返回的对象运行 LINQ 查询,因为查询运行非常缓慢。有什么方法可以获取从查询中生成的 SQL 语句?
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 的后代。
这有点复杂,因为 EF 不能在对象上使用我的辅助方法,所以我直接在查询中指定逻辑。
那么,有什么方法可以转储将由该 LINQ 查询生成的 SQL(例如到我的 log4net 存储库)?
最佳答案
您可以尝试使用 Entity Framework tracing provider如 described here (但它是 CTP3 的旧帖子)。
您的其他选择是:
在常见的 EF 中,您也可以使用
ToTraceString
正如@Andy 建议的那样,但 DbQuery
在 CodeFirst 中没有这个方法(或者我没有找到)。编辑:
所以
DbQuery
没有 ToTraceString
因为它是 directly implemented如 ToString
.
关于sql - 使用 Entity Framework CTP 5 "code only"时,如何获取 LINQ 查询背后的原始 SQL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5153776/