2011-08-03 2 views
1

ASP.NET MVC3 어플리케이션으로 데이터베이스 프로파일 링을 설정하려고합니다. 나는 이것에 대해 찾을 수있는 모든 블로그를 따랐다 그 결과는 다음과 같습니다코드를 처음 사용할 때 MvcMiniProfiler가 데이터베이스 프로파일 링을 표시하지 않습니다.

<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(); 
    } 
} 
의 Web.config에서

저장소 패턴을 사용 중이고 내 EF 코드가 MVC 응용 프로그램에서 참조하는 다른 프로젝트에 처음으로 있습니다. .

내 데이터베이스 클래스는 다음과 같습니다

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(); 
    } 
} 

이 내 응용 프로그램을 실행할 때 프로파일이 전혀 데이터베이스 프로파일이 표시되지 않습니다, 단지 일반 실행 : 같은

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(); 
    } 
} 

내 데이터베이스 공장이 보인다 컨트롤러/뷰의 시간.

도움을 주시면 감사하겠습니다.

감사

+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

당신의 공장 및 기타 코드를 위해 Application_Start에서이 코드를 설치하는 것은 너무 늦다는 것을 의심 날 리드 BEFORE 모든 EF 물건을 실행해야합니다 EF/코드 첫째, 국가의 노트.

[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

@ 토마스,'Bootstrapper.Run()'줄은 무엇입니까? MiniProfiler 코드보다 먼저 필요합니까? – counsellorben

+0

이것은 단지 경로, 필터 및 기타 시작 작업을 설정합니다. 실제로 코드에서 가져 와서 응용 프로그램 시작 부분에 보관했습니다. AppStart_MiniProfiler.Start()는 다른 모든 것보다 먼저 실행됩니다. – Thomas

관련 문제