挂钩IDbInterceptor到EntityFramework DbContext只有一次 [英] Hooking IDbInterceptor to EntityFramework DbContext only once

查看:236
本文介绍了挂钩IDbInterceptor到EntityFramework DbContext只有一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

IDbCommandInterceptor 界面没有很好的记录。我只发现了一些稀缺的教程:

The IDbCommandInterceptor interface is not very well documented. And I've only found a few scarce tutorials on it:

  • http://www.entityframeworktutorial.net/entityframework6/database-command-interception.aspx
  • https://msdn.microsoft.com/en-us/data/jj556606%28v=vs.113%29.aspx
  • https://entityframework.codeplex.com/wikipage?title=Interception
  • https://www.tutorialspoint.com/entity_framework/entity_framework_command_interception.htm
  • https://msdn.microsoft.com/en-us/data/dn469464%28v=vs.113%29.aspx

还有几个问题:

  • Entity Framework 6 - Timing queries
  • Getting DbContext from implementation of IDbCommandInterceptor

这些是挂钩我的建议发现:

These are the suggestions on hooking I've found:

1 - 静态 DbInterception 类:

DbInterception.Add(new MyCommandInterceptor());

2 - 在中执行上述建议DbConfiguration class

2 - Doing the above suggestion in a DbConfiguration class

public class MyDBConfiguration : DbConfiguration {
    public MyDBConfiguration() {
        DbInterception.Add(new MyCommandInterceptor());
    }
}

3 - 使用配置文件:

3 - Using the config file:

<entityFramework>
  <interceptors>
    <interceptor type="EFInterceptDemo.MyCommandInterceptor, EFInterceptDemo"/>
  </interceptors>
</entityFramework>






虽然我无法弄清楚如何勾DbContext的 DbConfiguration 类,并且在配置方法的类型中也没有。 另一个我发现的例子似乎建议你写一个记录器的命名空间:


Although I couldn't figure out how to hook the DbConfiguration class to the DbContext, and neither what to put in the type part of the config method. Another example I found seemed to suggest that you write the namespace of a logger:

type="System.Data.Entity.Infrastructure.Interception.DatabaseLogger, EntityFramework"

我注意到 DataBaseLogger 实现 IDisposable IDbConfigurationInterceptor

IDbInterceptor IDbCommandInterceptor 还实现 IDbInterceptor ,所以我尝试(没有成功)格式化如下:

I noted that DataBaseLogger implements IDisposable, IDbConfigurationInterceptor and
IDbInterceptor. IDbCommandInterceptor also implements IDbInterceptor, so I tried (without success) to format it like this:

type="DataLayer.Logging.MyCommandInterceptor, DataLayer"

当我直接调用静态 DbInterception 类时,每次调用都添加了另一个拦截器。所以我的快速肮脏的解决方案是利用静态构造函数:

And when I called the static DbInterception class directly, it added another interceptor every call. So my quick and dirty solution was to utilize static constructors:

//This partial class is a seperate file from the Entity Framework auto-generated class,
//to allow dynamic connection strings
public partial class MyDbContext // : DbContext
{
    public Guid RequestGUID { get; private set; }

    public MyDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
    {
        DbContextListeningInitializer.EnsureListenersAdded();

        RequestGUID = Guid.NewGuid();
        //Database.Log = m => System.Diagnostics.Debug.Write(m);
    }

    private static class DbContextListeningInitializer
    {
        static DbContextListeningInitializer() //Threadsafe
        {
            DbInterception.Add(new MyCommandInterceptor());
        }
        //When this method is called, the static ctor is called the first time only
        internal static void EnsureListenersAdded() { }
    }
}

但是什么是正确/预期的方法?

But what are the proper/intended ways to do it?

推荐答案

我发现我的 DbContext 类只需要具有 DbConfigurationType 属性,在运行时附加一个配置:

I figured out that my DbContext class just needed to have the DbConfigurationType attribute, to attach a configuration at runtime:

[DbConfigurationType(typeof(MyDBConfiguration))]
public partial class MyDbContext // : DbContext
{
    public MyDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
    { }
}

public class MyDBConfiguration : DbConfiguration {
    public MyDBConfiguration() {
        this.AddInterceptor(new MyCommandInterceptor());
    }
}

这篇关于挂钩IDbInterceptor到EntityFramework DbContext只有一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆