Watching the SQL queries Entity Framework executes is quite useful when a slow query needs optimization. If you’re already using ASP.NET Core, you might have already noticed that the debugging stream already outputs these SQL queries for you. But, if you’re working with ASP.NET MVC or ASP.NET Web API projects, by default you won’t see the SQL queries executed by EF.
Even though, there’s a very simple trick to be able to see the actual SQL. Simply add the following code in your DbContext constructor.
Once this piece of code has been added, you will be able to see the SQL queries your code executes in the Output tab. Have a look at the screenshot below.
With the output generated by the Debugger.Log method it’s possible to find out slow queries. A good idea would be even to generate a neat report from these messages. However, for quick and dirty debugging, this trick works.