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