我正在尝试使用我的 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();
}
}
当我运行我的应用程序时,分析器根本不会显示任何数据库分析,只是控制器/视图的常规执行时间。
任何帮助表示赞赏。
谢谢