使用mvc-mini-profiler

Ric*_*est 13 asp.net-mvc entity-framework entity-framework-4.1 mvc-mini-profiler

我正在尝试使用带有EFCodeFirst的mvc-mini-profiler我正在创建一个DbProfiledConnection并将其传递给构造中的DbContext,如下所示.应用程序继续按预期工作,sql未向Profiler公开.

public class WebContext : DbContext
{
    static DbConnection _connection = new SqlConnection(ConfigurationManager.ConnectionStrings["WebContext"].ConnectionString);
    static DbConnection _profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(_connection);        

    public WebContext()
            : base(_profiledConnection, true)
    {   

    }
Run Code Online (Sandbox Code Playgroud)

我糟透了

我已修改它,以便在我的UnitOfWork中构建我的WebContext时,我传入ProfiledDbConnection

public UnitOfWork()
{             
    var profiledConnection = MvcMiniProfiler.Data.ProfiledDbConnection.Get(connection);
    this.context = new MyContext(profiledConnection);
}
Run Code Online (Sandbox Code Playgroud)

我已经检查过并且在Application_BeginRequest中设置了MiniProfier Current,当我尝试查询数据库时,它会返回ProfiledDbConnection,并在ProfiledDbProviderServices类中抛出错误.

 protected override string GetDbProviderManifestToken(DbConnection connection)
 {
     return tail.GetProviderManifestToken(connection);
 }
Run Code Online (Sandbox Code Playgroud)

此方法返回"提供程序未返回ProviderManifestToken字符串".错误

Mar*_*ell 7

怀疑这与静态字段初始化程序有关.Web应用程序上的连接永远不应该是静态的(但最多是请求特定的).

关键是:ProfiledDbConnection实际出现了什么?如果您当前正在进行概要分析(在当前请求上),则该Get方法ProfiledDbConnection仅返回a ,并且针对该MiniProfiler请求上的实例分析连接.

如果使用静态字段,则有两种情况:

  • 静态字段在没有请求上下文(或非开发人员请求上下文)的情况下初始化:不会发生分析,因为它MiniProfiler.Current是null
  • 静态字段已初始化,但所有内容都是根据第一个请求记录的,该请求很快就会死亡