MvcMiniProfiler on EF 4.1 Code-First project doesn

2019-01-13 07:09发布

问题:

I have version 1.6 of the MvcMiniProfiler referenced (via Nuget) and have set everything up as described on the project homepage at http://code.google.com/p/mvc-mini-profiler/.

I have the following code in the Web.config:

<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.6.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
    </DbProviderFactories>
</system.data>

(The project homepage has Version=1.5.0.0 - the NuGet package has since been updated)

I have the following code in the Global.asax (and connection string also defined in Web.config):

    protected void Application_Start()
    {
        Log.Info("ReCoupon has started.");

        AreaRegistration.RegisterAllAreas();

        RegisterGlobalFilters(GlobalFilters.Filters);
        RegisterRoutes(RouteTable.Routes);

        var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["ReCouponContext"].ConnectionString);
        var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory);
        Database.DefaultConnectionFactory = profiled;

        Database.SetInitializer(new ReCouponContextInitializer());
    }

The profiler works great except that I can't get it to profile SQL. I am using SQL Server 2008 Express. I've been following the related issues on the Google Code project homepage and am totally stuck.

回答1:

This one had me stumped for a long time too. It appears that the connection string naming convention takes precedence over Database.DefaultConnectionFactory.

Could you try renaming the connection string in the web.config?

from

   <connectionStrings>
       <add name="ReCouponContext" connectionString="..." />
   </connectionStrings>

to

   <connectionStrings>
       <add name="ReCoupon" connectionString="..." />
   </connectionStrings>

and then change

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

to

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