我如何发现这语句生成实际的SQL? [英] How do I find out the actual SQL that this statement generates?

查看:122
本文介绍了我如何发现这语句生成实际的SQL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是用VS2010,.NET4和EF4。我想看到的时候,这是运行时生成实际的SQL。此外,这是什么来写这一说法的最好方法是什么?

下面是我的code:

  VAR cklContactItems =从在dbTestCenterViews.appvuChecklistExports
                                   其中,a.MarketChecklistID == MCLID
                                        &功放;&安培; a.checklistSectionID == SID
                                        &功放;&安培; a.fieldGroupOrder!= NULL
                                   排序依据a.fieldGroupOrder上升
                                   选择新{a.Column1,a.Column2,a.Column3,a.Column4, a.Column5,a.Column1FieldID,a.Column2FieldID,a.Column3FieldID,a.Column4FieldID,a.Column5FieldID,a.fieldGroupOrderLabel };
 

解决方案

  VAR的查询=(从X在context.MyEntity其中x ...选择x);

(查询作为的ObjectQuery< myEntity所>)。ToTraceString();
 

这将打印到跟踪日志...如果你想要一个简单的跟踪查看器(Visual Studio中的外)检查出的 DebugView中

和作为附加侧面说明,我已经用最好的实时探查/看出来有实体框架探查,你有为它付出,但它有一个试用版,它会给你的SQL匹配的code线。它还可以识别常见的问题。设置是愚蠢的简单......所有你需要补充的是,在应用程序的初始化一个语句。


*的编辑更新,以显示如何与用户code做*

我想我能为你做所有的辛勤工作...)。由于您使用的annoynmous型见好就收落< T> 部分。

  VAR cklContactItems =从在dbTestCenterViews.appvuChecklistExports
                               其中,a.MarketChecklistID == MCLID
                                    &功放;&安培; a.checklistSectionID == SID
                                    &功放;&安培; a.fieldGroupOrder!= NULL
                               排序依据a.fieldGroupOrder上升
                               选择新{
                                   a.Column1,
                                   a.Column2,
                                   a.Column3,
                                   a.Column4,
                                   a.Column5,
                                   a.Column1FieldID,
                                   a.Column2FieldID,
                                   a.Column3FieldID,
                                   a.Column4FieldID,
                                   a.Column5FieldID,
                                   a.fieldGroupOrderLabel
                                  };

System.Diagnostics.Trace.WriteLine((查询作为的ObjectQuery).ToTraceString());
 

I an using VS2010, .NET4 and EF4. I would like to see the actual SQL that is generated when this is run. Also, what is this the best way to write this statement?

Here is my code:

var cklContactItems = from a in dbTestCenterViews.appvuChecklistExports
                                   where a.MarketChecklistID == MCLID 
                                        && a.checklistSectionID == SID 
                                        && a.fieldGroupOrder != null
                                   orderby a.fieldGroupOrder ascending
                                   select new { a.Column1, a.Column2, a.Column3, a.Column4, a.Column5,a.Column1FieldID,a.Column2FieldID,a.Column3FieldID,a.Column4FieldID,a.Column5FieldID,a.fieldGroupOrderLabel };

解决方案

var query = (from x in context.MyEntity where x... select x);

(query as ObjectQuery<MyEntity>).ToTraceString();

That will print to the trace log... if you want a simple trace viewer (outside of Visual Studio) check out DebugView

And as an additional side note, the best "real time" profiler I have used/seen out there is Entity Framework Profiler, you have to pay for it, but there is a trial version available, and it will give you SQL matched to the line of code. It also recognizes common "issues." Setup is stupid simple... all you have to add is one statement in the initializer of your application.


*Edit updated to show how to do it with users code *

I guess i can do all the hard work for you... ;). Since you use an annoynmous type just leave off the <T> part.

var cklContactItems = from a in dbTestCenterViews.appvuChecklistExports
                               where a.MarketChecklistID == MCLID 
                                    && a.checklistSectionID == SID 
                                    && a.fieldGroupOrder != null
                               orderby a.fieldGroupOrder ascending
                               select new {
                                   a.Column1, 
                                   a.Column2, 
                                   a.Column3, 
                                   a.Column4,
                                   a.Column5,
                                   a.Column1FieldID,
                                   a.Column2FieldID,
                                   a.Column3FieldID,
                                   a.Column4FieldID,
                                   a.Column5FieldID,
                                   a.fieldGroupOrderLabel 
                                  };

System.Diagnostics.Trace.WriteLine((query as ObjectQuery).ToTraceString());

这篇关于我如何发现这语句生成实际的SQL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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