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!
there are several approaches to looking at the sql.
Free
- On the ObjectQuery do .ToTraceString() that will show u the sql generated for the query.
- Download ef tracing provider written by one of the EF team members. EF Tracing Provider
- Linq To Entities visualizer which you can download here.
- LinqPad
- Sqlserver profiler
Commercial
- Efprof.com
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.
See my answer here: Linq & String.ToLower() strange behavior
It allows SQL to be dumped to trace, or wherever you like.
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.
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.
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