使用代码优先和存储库模式时,MvcMiniProfiler 将不会显示数据库分析
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通过安装 Nuget MiniProfiler 和 MiniProfiler.EF 包解决。在 Global.asax 中添加以下内容
,最后将以下代码添加到标记的 JQuery 下方的头标记中。
你已经准备好了。就像魅力一样。
Solved by installing the Nuget MiniProfiler and MiniProfiler.EF package. Add the following in the Global.asax
And finally add the below code to the head tage below JQuery of your markup.
You're set. Works like a charm.
EF / Code First 的注释指出,您的工厂和其他代码必须在任何 EF 内容之前运行,这让我怀疑在 Application_Start 中安装此代码为时已晚。尝试创建一个预启动类,如下所示:
在您的项目中创建一个 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:Create an App_Start folder in your project, and place this class in the App_Start folder.