4

I setup MiniProfiler.MVC3 - 1.7 package in my project yesterday. The Controller and view profiling is working fine, but the peice I'm really interested in is the SQL Profiling. I have not been able to get this to work. I'm using EF Code First with a SQL 2008 database.

I have followed all the suggestions in this post ..

mvcminiprofiler-on-ef-4-1-code-first-project-doesnt-profile-sql

In the miniprofiler.cs i have my SQL connection setup as...

 var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["CMDBMVC3"].ConnectionString);

My Web.config db connection is...

 <add name="CMDBMVC3" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|CMDB_MVC3.mdf;Initial Catalog=CMDB_MVC3;Trusted_Connection=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

If I put a breakpoint on the mini-profiler line it points to the correct db connection. I'm not sure what else to do at this point. I would appreciate any direction on how to get the SQL profiling working.

Community
  • 1
  • 1
Mark Buckley
  • 267
  • 1
  • 4
  • 14
  • Is anybody able to help me with this? I still cannot get the SQL profiling to work. Appreciate any assistance anyone can provide. – Mark Buckley Aug 07 '11 at 01:00

1 Answers1

0

I use EF Code first and the mini profiler within my Context constructor I create a new connection factory and pass this to the ProfiledDbConnectionFactory method this returns a profiled connection that you can then set as the DefaultConnectionFactory of the context.

public MyConext()
{
    var factory = new ConnectionFactory(); 
    var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory);
    Database.DefaultConnectionFactory = profiled;
}

The connection Facotry 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 the ProfiledDBProvider to the web config 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'd also check out the new MVC version of the nuget package that auto configs profiling as part of a global action filter.

Richard Forrest
  • 3,567
  • 2
  • 23
  • 32
  • Thank You very much for your response. I am using the MiniProfiler.MVC3 v1.7 already This version creates miniprofiler.cs in the App_Start folder. Mine is populated as follows 'code' public static void PreStart() { var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["CMDBMVC3"].ConnectionString); var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory); Database.DefaultConnectionFactory = profiled; } – Mark Buckley Aug 13 '11 at 14:36
  • I've actually tried miniprofiler.mvc3 v1.7 on 2 separate projects on two separate computers with the exact same response. No SQL profiling at all. I'm obviously doing something wrong I just can't figure out what it is. – Mark Buckley Aug 13 '11 at 14:50