使用代码优先和存储库模式时,MvcMiniProfiler 将不会显示数据库分析

发布于 2024-11-27 21:24:21 字数 2703 浏览 3 评论 0原文

我正在尝试使用 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();
    }
}

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

任何帮助表示赞赏。

谢谢

I am trying to setup database profiling with my ASP.NET MVC3 application. I have followed every blog I can find about this and the result is:

In 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>

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

In controller:

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

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

        return View();
    }
}

I am using the repository patterns and my EF Code first lives in another project that is referenced by the MVC application.

My database class looks like:

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

My database factory looks like:

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

When I run my application the profiler will not show any database profiling at all, just the regular execution time of the controller/view.

Any help is appreciated.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪也成诗 2024-12-04 21:24:21

通过安装 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()

你已经准备好了。就像魅力一样。

Solved by installing the Nuget MiniProfiler and MiniProfiler.EF package. Add the following in the Global.asax

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

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

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

And finally add the below code to the head tage below JQuery of your markup.

@MvcMiniProfiler.MiniProfiler.RenderIncludes()

You're set. Works like a charm.

晨曦÷微暖 2024-12-04 21:24:21

EF / Code First 的注释指出,您的工厂和其他代码必须在任何 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 文件夹中。

The notes for EF / Code First, state that your factory and other code must be run BEFORE any EF stuff, which leads me to suspect that installing this code in Application_Start is too late. Try creating a pre-start class, as follows:

[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;
        }
    }
}

Create an App_Start folder in your project, and place this class in the App_Start folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文