简体   繁体   中英

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?

Just start using server side sql-tracing. You can see exactly what the sql server is working through without adding overhead to the client.

Here is more information: http://msdn.microsoft.com/en-us/library/ms187929.aspx

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:

HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.Initialize();

EDIT

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

HibernatingRhinos.Profiler.Appender.EntityFramework.EntityFrameworkProfiler.InitializeOfflineProfiling(filename);

then load the resulting file into the profiler.

This seems like it should give you what you want.

Your best bet is a profiler, to count SQL calls only you could just use the built-in SQL profiler (not included in SQL Express though).

For EF6, you can use context.Database.Log

https://msdn.microsoft.com/en-us/library/dn469464(v=vs.113).aspx

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