【问题标题】:MvcMiniProfiler will not show database profiling when using code first and repository pattern使用代码优先和存储库模式时,MvcMiniProfiler 不会显示数据库分析
【发布时间】:2011-08-03 17:55:40
【问题描述】:

我正在尝试使用我的 ASP.NET MVC3 应用程序设置数据库分析。我关注了所有我能找到的关于此的博客,结果是:

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

当我运行我的应用程序时,分析器根本不会显示任何数据库分析,只会显示控制器/视图的常规执行时间。

感谢任何帮助。

谢谢

【问题讨论】:

  • 我遇到了同样的问题。 [链接]stackoverflow.com/questions/6889929/… 1.7 版本根据@counsellorben 给出的答案进行设置。和 thomas 一样,我仍然无法让 SQL 分析工作。

标签: asp.net-mvc-3 ef-code-first mvc-mini-profiler


【解决方案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 下面的 head 标记中。

@MvcMiniProfiler.MiniProfiler.RenderIncludes()

你准备好了。像魅力一样工作。

【讨论】:

    【解决方案2】:

    EF / Code First 的注释声明您的工厂和其他代码必须运行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 文件夹中。

    【讨论】:

    • @Thomas,Bootstrapper.Run() 的行是什么?是否需要在您的 MiniProfiler 代码之前?
    • 它只是设置路由、过滤器和其他一些启动任务。我实际上将它从您的代码中取出并保存在应用程序启动中。这样 AppStart_MiniProfiler.Start() 在其他所有操作之前运行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2013-03-13
    • 2023-04-10
    • 2014-10-25
    • 1970-01-01
    相关资源
    最近更新 更多