Mike Mike - 10 months ago 70
C# Question

How can I find how many database calls Entity Framework is making per incoming request?

I am running a C# 4.0 website that uses Entity Framework 4.0 for database interaction. I want to find the pages that are causing Entity Framework to make the most calls to the database (since the more calls, the slower the page is likely to be).

I'd add some sort of instrumentation to Entity Framework, have actual users operate the website for a while, then analyze some sort of log to find out what pages generated the most calls to the database.

Is there some kind of performance counter or other event that can be inspected to figure out when a database call has been made by Entity Framework?

Answer Source

you could try the Entity Framework Profiler (its own site is here), although this is not a free product, it does however have a 30 day free trial. And its written by one of the smartest guys around

It will however flag up issues, such as Select N+1 issues and warn you about bad practices.

From the blurb:

Entity Framework Profiler is a real-time visual debugger allowing a development team to gain valuable insight and perspective into their usage of Entity Framework. The product is architected with input coming from many top industry leaders within the OR/M community. Alerts are presented in a concise code-review manner indicating patterns of misuse by your application. To streamline your efforts to correct the misuse, we provide links to the problematic code section that triggered the alert

again from the blurb (in response to the comment):

Using the Entity Framework Profiler is easy. First, we need to make the application that we profile aware of the profiler. Then, just start the profiler.

Preparing an application to be profiled

Add a reference to the HibernatingRhinos.Profiler.Appender.dll assembly, located in the downloadable zip. In the application startup (Application_Start in web applications, Program.Main in Windows / console applications, or the App constructor for WPF applications), make the following call:



seems that you can initialize the profiler for offline profiling. use this to initialize it instead (from here):


then load the resulting file into the profiler.

This seems like it should give you what you want.