2011-08-03 20 views
1

我正在嘗試使用ASP.NET MVC3應用程序設置數據庫分析。我按照每一個博客,我可以找到有關這一點,其結果是:MvcMiniProfiler首先使用代碼和存儲庫模式時不會顯示數據庫分析

在web.config中:

<system.data> 
    <DbProviderFactories> 
    <remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" /> 
    <add name="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider" description="MvcMiniProfiler.Data.ProfiledDbProvider" type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler, Version=1.6.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" /> 
    </DbProviderFactories> 
</system.data> 

在Global.asax中:

protected void Application_Start() 
{ 
    Bootstrapper.Run(); 

    MiniProfiler.Settings.SqlFormatter = new SqlServerFormatter(); 

    var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["TemplateDB"].ConnectionString); 
    var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory); 

    Database.DefaultConnectionFactory = profiled; 
} 

protected void Application_BeginRequest() 
{ 
    if (Request.IsLocal) { MiniProfiler.Start(); } 
} 

protected void Application_EndRequest() 
{ 
    MiniProfiler.Stop(); 
} 

在控制器:

public ActionResult About() 
{ 
    var profiler = MiniProfiler.Current; 

    using (profiler.Step("call database")) 
    { 
     ProjectResult result = projectService.Create("slug"); 

     return View(); 
    } 
} 

我正在使用存儲庫模式,我的EF代碼首先存在於由MVC應用程序引用的另一個項目中。

我的數據庫類的樣子:

public class Database : DbContext 
{ 
    public Database(string connection) : base(connection) 
    { 
    } 

    public DbSet<Project> Projects { get; set; } 

    protected override void OnModelCreating(DbModelBuilder modelBuilder) 
    { 
     modelBuilder.Entity<Project>().Property(p => p.Slug).IsUnicode().IsRequired().IsVariableLength().HasMaxLength(64); 
    } 

    public virtual void Commit() 
    { 
     base.SaveChanges(); 
    } 
} 

我的數據庫廠的樣子:

public class DatabaseFactory : Disposable, IDatabaseFactory 
{ 
    private readonly string connectionString; 
    private Database database; 

    public DatabaseFactory(string connectionString) 
    { 
     Check.Argument.IsNotNullOrEmpty(connectionString, "connectionString"); 

     this.connectionString = connectionString; 
    } 

    public Database Get() 
    { 
     return database ?? (database = new Database(connectionString)); 
    } 

    protected override void DisposeCore() 
    { 
     if (database != null) 
      database.Dispose(); 
    } 
} 

當我運行我的應用程序分析器不會顯示任何數據庫分析可言,只是普通的執行控制器/視圖的時間。

任何幫助表示讚賞。

感謝

+0

我遇到了同樣的問題。 [鏈接] http://stackoverflow.com/questions/6889929/mvc-mini-profiler-v1-7-on-ef-4-1-code-first-project-doesnt-profile-sql [/ link] 1.7版本根據@counsellorben給出的答案設置。像托馬斯一樣,我仍然無法使SQL分析工作。 –

回答

1

通過安裝Nuget MiniProfiler和MiniProfiler.EF包來解決。在Global.asax中添加以下代碼

protected void Application_Start() 
{ 
    MiniProfilerEF.Initialize(); 
} 

protected void Application_BeginRequest() 
{ 
    if (Request.IsLocal) 
    { 
     MiniProfiler.Start(); 
    } 
} 

protected void Application_EndRequest() 
{ 
    MiniProfiler.Stop(); 
} 

最後,將下面的代碼添加到您的標記的JQuery下面的標題。

@MvcMiniProfiler.MiniProfiler.RenderIncludes() 

您已設置。奇蹟般有效。

0

爲EF /代碼首先,國家的音符,你的工廠和其他代碼必須運行BEFORE任何EF的東西,這使我懷疑,在安裝此的Application_Start代碼是爲時已晚。嘗試創建啓動前類,如下所示:

[assembly: WebActivator.PreApplicationStartMethod(typeof(MyApp.App_Start.AppStart_MiniProfiler), "Start")] 

namespace MyApp.App_Start 
{ 

    [INSERT using DECLARATIONS] 

    public static class AppStart_MiniProfiler 
    { 
     public static void Start() 
     { 
      Bootstrapper.Run(); 

      MiniProfiler.Settings.SqlFormatter = new SqlServerFormatter(); 

      var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["TemplateDB"].ConnectionString); 
      var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory); 

      Database.DefaultConnectionFactory = profiled; 
     } 
    } 
} 

在項目中創建一個文件夾App_Start,並把這個類在App_Start文件夾中。

+0

我試過你的方法,但沒有任何運氣。 – Thomas

+0

@Thomas,Bootstrapper.Run()是什麼行?它是否需要在您的MiniProfiler代碼之前? – counsellorben

+0

它只是設置路線,過濾器和其他一些啓動任務。我實際上是從代碼中拿出來的,並保存在應用程序的開始。這種方式AppStart_MiniProfiler.Start()在一切之前運行。 – Thomas

相關問題