简体   繁体   English

EF Profiler - 它是如何工作的?

[英]EF Profiler - How does it work?

I'm trying to learn more about trying to tap into the ADO.Net / EF pipeline and how to unintuitively hook into the pipeline (to dump queries, results, etc to my log). 我正在尝试了解更多有关尝试使用ADO.Net / EF管道以及如何不直观地挂钩管道(将查询,结果等转储到我的日志)的更多信息。

Everything I have seen says that there is no way to unobtrusively or without using SQL Profiler... But I know its possible as the best EF Profiler manages to do it. 我所看到的一切都说没有办法不引人注目或没有使用SQL Profiler ......但我知道它可能是最好的EF Profiler设法做到的。

I do not know how exactly EfProf works internally, but EF is extensible in the form of custom providers - using a custom provider you can wrap an existing provider (ie SQL Server) and inspect all traffic - this allows profiling. 我不知道EfProf在内部是如何工作的,但EF可以自定义提供者的形式扩展 - 使用自定义提供程序,您可以包装现有提供程序(即SQL Server)并检查所有流量 - 这允许分析。 This article focuses on this topic: Tracing and Caching Provider Wrappers for Entity Framework - there's sample code provided. 本文重点介绍此主题: 实体框架的跟踪和缓存提供程序包装器 - 提供了示例代码。

Whenever you issue a LINQ or Entity SQL query through an ObjectContext instance, the query passes through a series of layers (see the picture below). 每当您通过ObjectContext实例发出LINQ或Entity SQL查询时,查询都会通过一系列图层(请参见下图)。 At high level we can say that all queries and updates from ObjectContext are translated and executed through EntityConnection, which in turns talks to server-specific data provider such as SqlClient or Sql Server CE client. 在高级别,我们可以说ObjectContext的所有查询和更新都是通过EntityConnection进行翻译和执行的,而EntityConnection又与服务器特定的数据提供程序(如SqlClient或Sql Server CE客户端)进行通信。

Provider interface used by Entity Framework is stackable, which means it's possible to write a provider which will wrap another provider and intercept communication between Entity Framework and the original provider. 实体框架使用的提供程序接口是可堆叠的,这意味着可以编写一个提供程序,该提供程序将包装另一个提供程序并拦截实体框架与原始提供程序之间的通信。

The wrapper provider gets a chance do interesting things, such as: 包装提供程序有机会做有趣的事情,例如:

Examining query trees and commands before they are executed 在执行查询树和命令之前检查它们

Controlling connections, commands, transactions, data readers 控制连接,命令,事务,数据读取器

在此输入图像描述

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

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