mvc-mini-profiler configuration clarification

2019-04-17 14:16发布

问题:

The profiler is installed in my MVC 3 app and it works, but what I'm having a hard time with is the correct way to get the EF 4 db portion setup correctly.

from the home page of the profiler

Use a factory to return your connection:

public static DbConnection GetOpenConnection()
{
    var cnn = CreateRealConnection(); 

    // wrap the connection with a profiling connection that tracks timings 
    return MvcMiniProfiler.Data.ProfiledDbConnection.Get(cnn, MiniProfiler.Current);
}

Entity Framework

public static MyModel Get()
{
    var conn = ProfiledDbConnection.Get(GetConnection());
    return ObjectContextUtils.CreateObjectContext<MyModel>(conn);
}

Ok, so right off the bat I'd like clarification whether the call to GetConnection() of the MyModel Get method should read GetOpenConnection()?

Then if it is a typo, what would the CreateRealConnection look like? I am using ODP.NET via the provider model and my Model Library does not have a ref to Oracle.DataAccess.Client and I'd prefer to keep it that way if I can.

Also, where would all this code reside in my repository?

public IQueryable<PRODUCTHEADER> Products
{
    get{ return ctx.PRODUCTHEADERs.AsQueryable(); }
}

Thank you, Stephen

回答1:

I was able to take pieces from this post MiniProfiler with EF "model first" edmx model and stitch together a working example for database first. Now granted this code may need to be pulled up and out of the controller, but it's working as advertised.

As a side note, my original question asked if there was a typo in the documentation and I don't believe so, there a 2 distinct usages and they are NOT dependent upon each other like I thought.

mvc-mini-profiler FTW!

Regards, Stephen

    public ActionResult Index()
    {
        // http://code.google.com/p/mvc-mini-profiler/
        // https://stackoverflow.com/questions/6802855/miniprofiler-with-ef-model-first-edmx-model

        var profiler = MiniProfiler.Current;

        var pConn = GetConnection();
        var context = pConn.CreateObjectContext<ChinookEntities>();

        using (profiler.Step("Doing complex stuff"))
        {
            using (profiler.Step("Step A"))
            {
                Thread.Sleep(100);
            }
            using (profiler.Step("Step B"))
            {
                Thread.Sleep(250);
            }
            using (profiler.Step("Step C"))
            {
                var result = context.Albums.AsQueryable()
                    .OrderBy(a => a.ArtistId).First();

                return View(result);
            }
        }
    }

    private static DbConnection GetConnection()
    {
        // A SqlConnection, SqliteConnection ... or whatever
        var connectionString = ConfigurationManager.ConnectionStrings["ChinookEntities"].ConnectionString;
        var ecsb = new EntityConnectionStringBuilder(connectionString);
        var sqlConn = new SqlConnection(ecsb.ProviderConnectionString);
        // wrap the connection with a profiling connection that tracks timings 
        return ProfiledDbConnection.Get(sqlConn, MiniProfiler.Current);
    }


回答2:

I was going to say that the mini-profiler was 4.0 only, but its been recently updated. This might help (although you are not code first)

Using mvc-mini-profiler database profiling with Entity Framework Code First