13

我正在尝试在EFCodeFirst中使用mvc-mini-profiler我创建了一个DbProfiledConnection并将其传递给DbContext,如下所示。该应用程序继续按预期工作,但不会暴露给Profiler。使用mvc-mini-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) 
    { 

    } 

oops my bad。

我已经修改了它,这样,当我WebContext在我的UnitOfWork构建我在ProfiledDbConnection通过

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

我检查和MiniProfier目前已被的Application_BeginRequest设置并返回一个ProfiledDbConnection当我然后尝试查询数据库,在ProfiledDbProviderServices类中抛出错误。

protected override string GetDbProviderManifestToken(DbConnection connection) 
{ 
    return tail.GetProviderManifestToken(connection); 
} 

此方法返回“提供程序没有返回ProviderManifestToken字符串”。错误

+6

顺便说一句,一个静态C连接是危险的 - 它应该是特定于请求的。 – 2011-06-09 11:08:46

+0

为什么静态连接很危险?另外,如果我只在控制器的顶部启动新的上下文,而不是在每个请求中启动一个新的上下文,我是否实际上使用了静态上下文? – sirtimbly 2011-06-27 19:56:38

回答

7

嫌疑人这涉及到静态字段初始值设定项。无论如何,web应用上的连接应该是永远不会是(但最多只能请求特定)。

关键是:ProfiledDbConnection究竟是怎么出来的?仅当您正在分析(在当前请求中)时,Get方法才会返回ProfiledDbConnection,并根据该请求针对MiniProfiler实例对连接进行概要分析。

如果您使用的是静态字段,那么有两种情况:

  • 静态字段没有初始化的请求上下文(或者非开发人员的请求上下文):无纹会出现如MiniProfiler.Current为null
  • 静态字段被初始化,但一切都记录针对第一个请求,这是快死
+0

大+1。用静态上下文引用分析应用程序没有什么意义。你可以通过查看它会遇到问题来判断! – 2011-06-09 12:28:04

+0

oops我的坏。我已经改变了我的代码,但现在在ProfiledDbProviderServices – 2011-06-09 16:07:26

+1

@feanz啊正确;好的,EF codefirst不是我使用的工具,所以我没有验证该场景。但是,我们已经(今天)有一位用户首先为EF代码自愿提供补丁。给我几个小时合并等 – 2011-06-09 16:25:23