1

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

アプリケーションを実行すると、プロファイラーはデータベースのプロファイリングをまったく表示せず、コントローラー/ビューの通常の実行時間のみを表示します。

どんな助けでも大歓迎です。

ありがとう

4

2 に答える 2

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

あなたは設定されています。魅力のように機能します。

于 2011-09-01T17:47:37.890 に答える
0

EF / Code First のメモには、工場やその他のコードでBEFOREEF のものを実行する必要があると記載されているため、このコードを 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 フォルダーに配置します。

于 2011-08-03T19:30:10.280 に答える