简体   繁体   中英

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

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

你想要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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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