First, I use the EF code and the mini profiler in my context constructor. I create a new factory connection and pass it to the ProfiledDbConnectionFactory method, this returns a profiled connection, which can then be set as the DefaultConnectionFactory context.
public MyConext() { var factory = new ConnectionFactory(); var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory); Database.DefaultConnectionFactory = profiled; }
Facotry connection just returns a new sql connection
public class ConnectionFactory :IDbConnectionFactory { public DbConnection CreateConnection() { var cnn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["SomeConnection"].ToString()); return cnn; }
You also need to add ProfiledDBProvider to the web configuration file. Make sure the version number is correct for you.
<system.data> <DbProviderFactories> <remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" /> <add name="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider" description="MvcMiniProfiler.Data.ProfiledDbProvider" type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler, Version=1.7.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" /> </DbProviderFactories> </system.data>
This works fine for me in both MVC and asp.net webforms using the Miniprofiler nuget package. I also checked out the new MVC version of nuget, which automatically configures profiling as part of a global action filter.
source share