half-ogre
half-ogre

Reputation: 594

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?

Upvotes: 7

Views: 1623

Answers (1)

davidfowl
davidfowl

Reputation: 38764

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

Upvotes: 6

Related Questions