MiniProfiler setup - A null was returned after cal

2019-07-18 03:00发布

问题:

After hours of struggling with MiniProfiler to make it profile the Database queries, I have no luck and I'm getting the error:

A null was returned after calling the 'get_ProviderFactory' method on a store provider instance of type 'StackExchange.Profiling.Data.ProfiledDbConnection'. The store provider might not be functioning correctly.

I got through many SO posts but nothing has worked so far, like this post which is about the same error but with different configuration and well there's not an answer. Now I know that ProfiledDbConnection is not overriding DbProviderFactory and it's parent class DbConnection returns null in it's implementation of DbProviderFactory so the error is expectable but it should work somehow. There is a MiniProfilerEF.Initialize() but it seams to be usable for CodeFirst only and I'm using DatabaseFirst approach.

I have installed MiniProfiler, MiniProfiler.EF, MiniProfilerMVC3 nuget packages. And here is my code:

        string connectionString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
        var sqlConnection = new SqlConnection(connectionString);
        var profiled = new ProfiledDbConnection(sqlConnection, MiniProfiler.Current);
        var db = new DbContext(profiled, true);
        db.Set<Customer>().ToList();

And I'm using asp.net mvc4, EF5, DatabseFirst, MiniProfiler 2.1.0.0, Sql Server

Any Idea?

回答1:

So, coming from your comment on So, coming from your comment on Setup of mvc-mini-profiler for EF-db- first, have you tried removing the Mini Profiler-specific wrapper?

var profiled = new ProfiledDbConnection(sqlConnection, MiniProfiler.Current);
var db = new DbContext(profiled, true);
db.Set<Customer>().ToList();

Instead, just adding

protected void Application_Start()
{
    // any other code
    MiniProfilerEF.Initialize();
}

and then doing standard db access?

using (var db = new WordsEntities()) {
    var posts = db.Customer.Take(4);
    // more code
}