简体   繁体   English

如何找到 Entity Framework 对每个传入请求进行的数据库调用次数?

[英]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.我正在运行一个使用 Entity Framework 4.0 进行数据库交互的 C# 4.0 网站。 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).我想找到导致 Entity Framework 对数据库进行最多调用的页面(因为调用越多,页面可能越慢)。

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.我会在 Entity Framework 中添加某种工具,让实际用户操作网站一段时间,然后分析某种日志以找出哪些页面对数据库的调用最多。

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.只需开始使用服务器端 sql-tracing。 You can see exactly what the sql server is working through without adding overhead to the client.您可以准确地看到 sql 服务器正在处理什么,而不会增加客户端的开销。

Here is more information: http://msdn.microsoft.com/en-us/library/ms187929.aspx这里有更多信息: 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.你可以试试Entity Framework Profiler (它自己的网站在这里),虽然这不是免费产品,但它有 30 天的免费试用期。 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.但是,它会标记问题,例如 Select N+1 问题,并警告您不良做法。

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. Entity Framework Profiler 是一个实时可视化调试器,允许开发团队获得有价值的洞察力和视角来了解他们对 Entity Framework 的使用。 The product is architected with input coming from many top industry leaders within the OR/M community.该产品的架构来自 OR/M 社区中许多顶级行业领导者的意见。 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.使用 Entity Framework Profiler 很容易。 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.添加对 HibernatingRhinos.Profiler.Appender.dll 程序集的引用,该程序集位于可下载的 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:在应用程序启动中(web 应用程序中的 Application_Start,Windows / 控制台应用程序中的 Program.Main,或 WPF 应用程序的 App 构造函数),进行以下调用:

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).你最好的选择是一个分析器,要计算 SQL 调用,你只能使用内置的 SQL 分析器(不包括在 Z9778840A0100CB30C9828 Express 虽然76741B0B5A2Z 中)。

For EF6, you can use context.Database.Log对于 EF6,您可以使用context.Database.Log

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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