c# - 通过 IDbCommandInterceptor 检测或传递 Entity Framework 6 和 Entity Framework Core 中的调用方法
问题描述
我有兴趣捕获启动实体框架查询的方法的名称。我将在 Entity Framework 6 和 Entity Framework Core 中使用此功能
考虑以下类:
命名空间 MyCompany.MyApp { 公共类 MyDataClass { // ... 剪断 ... public Person GetPerson(long id) { 返回 _dbContext 。人 .FirstOrDefault(p => p.Id == id); } } }
以及以下拦截器:
public class EFCommandInterceptor : IDbCommandInterceptor
{
// ... snip other interface methods ...
public void ReaderExecuted(System.Data.Common.DbCommand command, DbCommandInterceptionContext<System.Data.Common.DbDataReader> interceptionContext)
{
var callingMehtod = GetCallingMethod() // <--- I'd like this to return the string "MyCompany.MyApp.MyDataClass.GetPerson"
}
}
有什么方法可以确定是什么方法启动了导致调用拦截器的查询?
由于我将使用此功能进行跟踪和性能分析,因此我无法发送和记录Environment.StackTrace
通过系统运行的每个查询的全部内容。
我还考虑StackTrace
过结合使用该类stackTrace.GetFrames()
来尝试分析调用堆栈以找到启动查询的实际方法,但目前尚不清楚如何在不依赖某种通用标识数据访问类的命名空间约定的情况下可靠地执行此操作.
另一种可以接受的方法可能类似于:
命名空间 MyCompany.MyApp { 公共类 MyDataClass { // ... 剪断 ... public Person GetPerson(long id) { 返回 _dbContext 。人 .WithExtraInterceptorContext(new { CallingMethod = "MyCompany.MyApp.MyDataClass.GetPerson"}) // <--- 可以这样做吗? .FirstOrDefault(p => p.Id == id); } } }
上面示例的意图是稍后能够通过DbCommandInterceptionContext
内部检索额外的上下文IDbCommandInterceptor
最终,我要解决的问题是在没有完整调用堆栈的情况下获取启动查询的方法的完全限定名称。
解决方案
使用 EF Core,您可以使用 TagWith
var result = db.Albums
.TagWith(MethodBase.GetCurrentMethod().Name)
.ToList();
推荐阅读
- python - 有没有办法在检测屏幕上显示检测到的物体数量?
- android - TextToSpeech API 中的 speak 方法是否将数据发送到 Google 服务器?
- c# - 部分视图控制器中的重定向操作不起作用。(错误 - 不允许子操作执行重定向操作。)
- python - Kivy 代码在 kivy 启动器上运行时崩溃
- python - 用最后一个有效值的百分比填充 nan-s
- javascript - 如何使用 javascript 正则表达式从 url 中仅获取主机名?
- android - Google Play 服务已过时。需要 12800000 但找到 8703448
- android - 如何在 kotlin parceable 中使用 Any 对象?
- javascript - 这两种包含 Javascript 的方法是否等效?
- python-3.x - 字符串化查询集