I have a web application that is using Entity Framework to query a SQL Server database. I would like to be able to know programmatically how many queries (round-trips) were executed on the database. The idea is to log that information to easily detect errors where a relationship was not included and caused lots of round-trips.
Is there a way to achieve this? I don’t mind if the solution is specific to SQL Server.
Note: I want to monitor the database programmatically, therefore tools such as SQL Server Profiler are no use to me. I want to be able to know, at the end of a request and in the code that handles the request, how many queries were executed by that request.
You can use the provider statistics feature of the SQL connection.