How to configure mvc-mini-profiler to work with DbContext, which passes the name of the connection string to the database?

My DbContext ctor looks like this:

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

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

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

If I delete the connection string in my DbContext ctor, I get the profiling as expected. But I do not want to specify my connection string in accordance with the EF standard. What do I need to change to get mvc-mini-profiler to work with my use of DbContext?

+7
source share
1 answer

You can pass in 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); } } 
+6
source

All Articles