Entity Framework Generated SQL

Is it possible to output SQL generated by the Entity Framework at the global level, and not based on query by query? I am looking to just dump it all so that I can view it.

If this is not possible, how can I see how SQL is created for updates and inserts?

+6
c # sql entity-framework
source share
4 answers

you want LinqPad , here are some videos that show you how to use it

+3
source share

SQL Server Profiler allows you to view the commands that are executed on the server while the application is running.

Another free SQL Server 2005 Express profiling tool is here .

UPDATE

Another way to see what LINQ is creating is the Log property for the DataContext.

This is a TextWriter that should be easy to save content to a file or redirect to Console.Out.

MSDN Information for the Log Property

+7
source share

I was also looking for an answer. Turns out there is a pretty great way to view the EF generated SQL code if you don't mind plunging into the somewhat sketchy world of reflection.

A very resourceful poster on the MSDN forums wrote a set of extension methods that allow you to unload SQL output from an ObjectContext (i.e. the material that will be executed when SaveChanges() called).

You can find the link here - find the g_yordanov message containing the CustomExtensions class.

I tested this in the last little time, and it seems like a pretty trick. The only catch was that I had to make the correction suggested by David Cater in this thread - changing the Dictionary<long, object> to Dictionary<int, object> .

+1
source share

Since this was originally set, you can also use EFProf to profile the Entity Framework application, which allows you to see SQL Created among many other metrics.

+1
source share

All Articles