简体   繁体   English

实体框架生成的SQL

[英]Entity Framework Generated SQL

Is it possible to output the SQL generated by the Entity Framework on a global level and not on a query by query basis? 是否可以在全局级别而不是按查询的基础上输出实体框架生成的SQL? I'm looking to just dump it all somewhere so I can review it. 我希望把它全部丢弃,以便我可以查看它。

If that is not possible, how do I view the SQL being generated for updates and inserts? 如果这不可能,我如何查看为更新和插入生成的SQL?

The SQL Server Profiler will allow you to view the commands that are being executed on the server while the application is running. SQL Server Profiler允许您在应用程序运行时查看服务器上正在执行的命令。

Another free tool for profiling SQL Server 2005 Express here . 另一个用于在此处分析SQL Server 2005 Express的免费工具。

UPDATE UPDATE

Another method to see what is being generated by LINQ is the Log property of the DataContext. 查看LINQ生成内容的另一种方法是DataContext的Log属性。

It is a TextWriter that should be easy to save the contents to a file or redirect to Console.Out. 它是一个TextWriter,应该很容易将内容保存到文件或重定向到Console.Out。

MSDN Info for Log property Log属性的MSDN信息

你想要LinqPad ,这里有一些视频 ,告诉你如何使用它

由于最初这是问题,您现在还可以使用EFProf来分析您的Entity Framework应用程序,它允许您在许多其他指标中查看SQL Generated。

I was looking for an answer to this too. 我也在寻找答案。 It turns out there is a pretty nifty way to view the EF-generated SQL, if you don't mind dipping into the somewhat-sketchy world of reflection. 事实证明,如果您不介意进入有点粗略的反射世界,那么查看EF生成的SQL有一种非常好的方式。

A very resourceful poster on the MSDN forums wrote a set of extension methods that let you dump the SQL output of an ObjectContext (ie the stuff that will get executed when you call SaveChanges() ). MSDN论坛上一张非常资源丰富的海报写了一组扩展方法,允许您转储ObjectContext的SQL输出(即调用SaveChanges()时将执行的内容)。

You can find the link here - look for the post by g_yordanov containing the CustomExtensions class. 你可以在这里找到链接 - 查找包含CustomExtensions类的g_yordanov的帖子。

I have been testing this out over the past little while and it seems to do the trick quite nicely. 我在过去的一段时间里一直在测试它,它似乎很好地完成了这个伎俩。 The one catch is that I had to make the fix suggested by David Cater in that thread - changing the Dictionary<long, object> to a Dictionary<int, object> . 唯一的问题是我必须在该线程中进行David Cater建议的修复 - 将Dictionary<long, object>更改为Dictionary<int, object>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM