使用mvc-mini-profiler数据库分析与Entity Framework Code First

rob*_*mzd 24 ef-code-first entity-framework-ctp5 mvc-mini-profiler

我正在使用ASP.Net MVC 3和Entity Framework代码优先构建的项目中使用mvc-mini-profiler.

一切都很有效,直到我尝试通过ProfiledDbConnection在文档中描述的方式包装连接来添加数据库分析.由于我使用的是DbContext,因此我尝试提供连接的方式是使用静态工厂方法构造函数:

public class MyDbContext : DbContext
{                
    public MyDbContext() : base(GetProfilerConnection(), true)
    { }

    private static DbConnection GetProfilerConnection()
    {
        // Code below errors
        //return ProfiledDbConnection.Get(new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionName"].ConnectionString));

        // Code below works fine...
        return new SqlConnection(ConfigurationManager.ConnectionStrings["MyConnectionName"].ConnectionString);
    }

    //...
}
Run Code Online (Sandbox Code Playgroud)

使用时ProfiledDbConnection,我收到以下错误:

ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.

堆栈跟踪:

[ArgumentException: The connection is not of type 'System.Data.SqlClient.SqlConnection'.]
   System.Data.SqlClient.SqlProviderUtilities.GetRequiredSqlConnection(DbConnection connection) +10486148
   System.Data.SqlClient.SqlProviderServices.GetDbProviderManifestToken(DbConnection connection) +77
   System.Data.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +44

[ProviderIncompatibleException: The provider did not return a ProviderManifestToken string.]
System.Data.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +11092901
   System.Data.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection) +11092745
   System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) +221
   System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) +61
   System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) +1203482
   System.Data.Entity.Internal.LazyInternalContext.InitializeContext() +492
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +26
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +89
   System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext() +21
   System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider() +44
   System.Linq.Queryable.Where(IQueryable`1 source, Expression`1 predicate) +135
Run Code Online (Sandbox Code Playgroud)

我已经介入,返回的类型ProfiledDbConnection.Get是类型ProfiledDbConnection(即使当前的MiniProfiler为null).

MiniProfiler.Start()方法是在全球范围内被称为Application_BeginRequest()该方法之前DbContext被实例化.我也为每个请求调用Start方法,但是如果用户的角色不正确则调用stop:

    protected void Application_BeginRequest()
    {
        // We don't know who the user is at this stage so need to start for everyone
        MiniProfiler.Start();
    }

    protected void Application_AuthorizeRequest(Object sender, EventArgs e)
    {
        // Now stop the profiler if the user is not a developer
        if (!AuthorisationHelper.IsDeveloper())
        {
            MvcMiniProfiler.MiniProfiler.Stop(discardResults: true);
        }
    }

    protected void Application_EndRequest()
    {
        MiniProfiler.Stop();
    }
Run Code Online (Sandbox Code Playgroud)

我不确定这是否会影响事情,但我也使用StructureMap作为IoC来DbContext使用以下初始化器:

For<MyDbContext>().Singleton().HybridHttpOrThreadLocalScoped();
Run Code Online (Sandbox Code Playgroud)

我知道这里有一个类似的问题,很好地解释了该用户正在发生的事情,但它似乎并没有解决我的问题.

编辑:

为清楚起见.我试图传递连接ProfiledDbConnection,以便从Entity Framework Code First配置生成的sql.

Profiled Sql

实体框架期望与类型的连接SqlConnection,当然这不是.

这是我的连接字符串的示例(请注意providerName)

<add name="MyDbContext" connectionString="Server=.\SQLEXPRESS; Database=MyDatabase;Trusted_Connection=true;MultipleActiveResultSets=true" providerName="System.Data.SqlClient" />
Run Code Online (Sandbox Code Playgroud)

我试图创建自己的ProfiledDbConnection继承版本,SqlConnection但它是一个密封的类.

如果有某种方式告诉实体框架有关自定义连接类型,那么这可能会起作用.我尝试providerName在连接字符串中设置MvcMiniProfiler.Data.ProfiledDbConnection但是没有用.

所以.也许这个问题的演变是:如何将自定义连接类型传递给Entity Framework Code First?

Sam*_*ron 13

现在完全支持,查看最新的源代码或从nuget获取包.

如果您使用nuget,则需要MiniProfiler.EF包.(1.9.1及以上)

支持这一点涉及对底层代理对象的大量修改,以支持充当EF代码优先代理.

要添加此支持:

在你Application_Start跑步期间:

MiniProfilerEF.Initialize();

注意:EF Code First将表元数据存储在名为:的表中EdmMetadata.此元数据使用提供程序作为实体键的一部分.如果您将提供程序初始化为非配置文件提供程序,则必须重新构建此元数据.删除所有行EdmMetadata可能会起作用,或者一些更聪明的提供者能够透明地处理这些行.

  • 也许你应该提一下,web.config中的删除和添加节点应该添加在<system.data>然后<DbProviderFactories>下 (2认同)