6

Is it possible to output the SQL generated by the Entity Framework on a global level and not on a query by query basis? 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?

0

4 Answers 4

7

The SQL Server Profiler will allow you to view the commands that are being executed on the server while the application is running.

Another free tool for profiling SQL Server 2005 Express here.

UPDATE

Another method to see what is being generated by LINQ is the Log property of the DataContext.

It is a TextWriter that should be easy to save the contents to a file or redirect to Console.Out.

MSDN Info for Log property

Sign up to request clarification or add additional context in comments.

3 Comments

Ideally, I'd like to output to a file or view the SQL in Visual Studio and avoid having to run profiler or use an additional tool.
LINQ to SQL and Entity Framework are not the same. Entity Framework context does not have this option.
Nice, never knew that about L2S, just another reason I'm glad I went with it over EF :)
3

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

3 Comments

LINQPad is great for selects, but I need this for all transactions (i.e. select, insert, update, delete) – Matthew 5 mins ago
LINQPad does all of those as well, just checked myself
Thank you, I see that now. I was hoping for a different solution but this will work.
1

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

2 Comments

EFProf is commercial software.
Do developers not use commercial software now?
1

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.

A very resourceful poster on the MSDN forums wrote a set of extension methods that let you dump the SQL output of an ObjectContext (i.e. the stuff that will get executed when you call SaveChanges()).

You can find the link here - look for the post by g_yordanov containing the CustomExtensions class.

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>.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.