Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I configure mvc-mini-profiler to work with a DbContext that passes the connection string name to the base?

My DbContext ctor looks like this:

public class FnordDbContext : DbContext
{
    public FnordDbContext() : base("Fnord")
    {
    }

    /* stuff */
}

And my mvc-mini-profiler bootstrapper looks like this:

var sqlConnectionFactory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["Fnord"].ConnectionString);
var profiledConnectionFactory = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(sqlConnectionFactory);
Database.DefaultConnectionFactory = profiledConnectionFactory;

If I remove the connection string in my DbContext ctor, I get profiling as expected. But I don't want to have to name my connection string according to EF's convention. What do I need to change to make mvc-mini-profiler work with my DbContext use?

like image 729
half-ogre Avatar asked Aug 08 '11 06:08

half-ogre


1 Answers

You can pass in the a ProfiledDbConnection explicitly to the ctor of your DbContext:

public class MyDbContext : DbContext {
    public MyDbContext()
        : base(GetProfiledConnection()) {
    }

    private static DbConnection GetProfiledConnection() {
        var connectionString = ConfigurationManager.ConnectionStrings["name"].ConnectionString;
        var connection = new SqlConnection(connectionString);
        return ProfiledDbConnection.Get(connection);
    }
}
like image 168
davidfowl Avatar answered Oct 20 '22 06:10

davidfowl