View EF4 generated queries?

爱⌒轻易说出口 提交于 2019-12-23 00:49:12

问题


LINQ-to-SQL had several ways, including a visualizer add-in, to view the generated SQL from an IQueryable.

I can't find the equivalent for Entity Framework 4. Nothing on StackOverflow, no blogs. How is it done?

Preferably, I'd like to be able to do it in code and without having to actually execute the query just to see it.

Thanks!


回答1:


there are several approaches to looking at the sql.

Free

  1. On the ObjectQuery do .ToTraceString() that will show u the sql generated for the query.
  2. Download ef tracing provider written by one of the EF team members. EF Tracing Provider
  3. Linq To Entities visualizer which you can download here.
  4. LinqPad
  5. Sqlserver profiler

Commercial

  1. Efprof.com



回答2:


If you can't get any of the other solutions to work, you could try using the SQL Server Profiler if you have access to the SQL Machine.

Within SQL Server Management Studio you can do the following:

Tools -> SQL Server Profiler.

Create a new Trace and run your code and you should see the queries come across. You can create some filters so you don't see the security / audit stuff which you probably don't care about.




回答3:


See my answer here: Linq & String.ToLower() strange behavior

It allows SQL to be dumped to trace, or wherever you like.




回答4:


Hi there is this visualizer...but I could not get it to work for me...you could try it..I would recommend LINQPad for viewing your queries you can setup your ef connection and execute your queries.




回答5:


Did you try this: http://blog.cincura.net/227674-how-to-show-sql-command-created-by-entity-framework/

Btw that was first result on Google.




回答6:


An MSDN blog hosts a sample trace provider which I used in an EF 3.5 project. Don't know whether it works in EF4, too.



来源:https://stackoverflow.com/questions/4136558/view-ef4-generated-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!