使用MiniProfiler和EF 5以及Autofac分析DbContext的正确方法

时间:2012-11-21 15:30:51

标签: autofac entity-framework-5 dbcontext mvc-mini-profiler

MiniProfiler site为生成实体框架ObjectContext提供了以下代码:

public static MyModel Get()
{
  var conn =  new StackExchange.Profiling.Data.EFProfiledDbConnection(GetConnection(), MiniProfiler.Current);
  return ObjectContextUtils.CreateObjectContext<MyModel>(conn); // resides in the MiniProfiler.EF nuget pack
}

但是,使用Entity Framework 5,我没有使用ObjectContext - 而是使用DbContext。我无法在此处插入模型名称,因为CreateObjectContext<T>()方法希望TObjectContext类型。 (出于同样的原因,this answer中给出的代码也不起作用)。

此外,我使用autofac来初始化我的Db连接。这是使用以下注册的(MyData =我的EF DataContext的名称):

Builder.RegisterType<MyData>().As<DbContext>().InstancePerHttpRequest();

结合两个部分:如何使用autofac初始化与MiniProfiler.EF绑定的DbContext?如果那是不可能的,至少我该怎么做第一部分(为MiniProfiler.EF创建工厂方法以返回DbContext)?

2 个答案:

答案 0 :(得分:11)

我刚刚开始工作:

public static class DbContextUtils
{
    private const BindingFlags PrivateInstance = BindingFlags.NonPublic | BindingFlags.Instance;

    public static T CreateDbContext<T>() where T : DbContext
    {
        return CreateDbContext<T>(GetProfiledConnection<T>());
    }

    public static T CreateDbContext<T>(this DbConnection connection) where T : DbContext
    {
        var workspace = new MetadataWorkspace(new[] { "res://*/" }, new[] { typeof(T).Assembly });
        var factory = DbProviderServices.GetProviderFactory(connection);

        var itemCollection = workspace.GetItemCollection(DataSpace.SSpace);
        var providerFactoryField = itemCollection.GetType().GetField("_providerFactory", PrivateInstance);
        if (providerFactoryField != null) providerFactoryField.SetValue(itemCollection, factory);

        var ec = new EntityConnection(workspace, connection);

        return CtorCache<T, DbConnection>.Ctor(ec);
    }

    public static DbConnection GetProfiledConnection<T>() where T : DbContext
    {
        var dbConnection = ObjectContextUtils.GetStoreConnection("name=" + typeof(T).Name);
        return new EFProfiledDbConnection(dbConnection, MiniProfiler.Current);
    }

    internal static class CtorCache<TType, TArg> where TType : class
    {
        public static readonly Func<TArg, TType> Ctor;
        static CtorCache()
        {
            var argTypes = new[] { typeof(TArg) };
            var ctor = typeof(TType).GetConstructor(argTypes);
            if (ctor == null)
            {
                Ctor = x => { throw new InvalidOperationException("No suitable constructor defined"); };
            }
            else
            {
                var dm = new DynamicMethod("ctor", typeof(TType), argTypes);
                var il = dm.GetILGenerator();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Newobj, ctor);
                il.Emit(OpCodes.Ret);
                Ctor = (Func<TArg, TType>)dm.CreateDelegate(typeof(Func<TArg, TType>));
            }
        }
    }
}

它基于MiniProfiler's ObjectContextUtils中的代码。

你这样使用它:

builder.Register(c => DbContextUtils.CreateDbContext<MyData>()).As<DbContext>().InstancePerHttpRequest();

此解决方案需要您的DbContext有一个构造函数,它接受DbConnection并将其传递给base,如下所示:

public MyData(DbConnection connection)
    : base(connection, true)
{
}

答案 1 :(得分:6)

DbContext课程有constructor,其中包含现有DbConnection

所以你需要在MyData上使用一个新的构造函数来调用基础

public class MyData : DbContext
{
    public MyData(DbConnection existingConnection, bool contextOwnsConnection)
        : base(existingConnection, contextOwnsConnection)
    {
    }

    //..
}

然后您使用MyData注册Register

builder.Register(c => 
{
   var conn =  new EFProfiledDbConnection(GetConnection(), MiniProfiler.Current);
   return new MyData(conn, true);
}).As<DbContext>().InstancePerHttpRequest();