15

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.

rkaregaran
  • 620
  • 5
  • 13
  • 1
    Hey @Sam Saffron, the solution below worked - I don't quite understand it. I can give you access to my bitbucket repo so that you can see it in action if you still want. – rkaregaran Jul 23 '11 at 17:24
  • Give 1.9.1 a shot. I finally have SQL profiling with EF now after updating. – RyanW Aug 29 '11 at 14:55

1 Answers1

7

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);
Chris Foster
  • 1,310
  • 11
  • 13
  • OMG - it worked like a charm. I'm not sure I understand what you mean with the naming convention taking precedence? – rkaregaran Jul 23 '11 at 17:21
  • EF code first will automatically look for connection strings that are named the same as your context class. If it doesn't find one, it will then look at Database.DefaultConnectionFactory. I'm not really sure what we're supposed to do if we want to profile more than one database though. – Chris Foster Jul 23 '11 at 18:33
  • Ahh, yeah I'm an idiot, got it - I was wondering how my context was still working - because now we're setting the default db connection. Yeah not sure on the multi-db thing either. – rkaregaran Jul 23 '11 at 18:39
  • 6
    This doesn't seem to work for me. When I rename the MyModelContext connection string to MyModel I get a login failure. This is caused by the fact that DefaultConnectionFactory will set the Server, Username and Password but it will not set the database name so EF is looking for a database called MyProject.MyModel.MyModelContext which obviously doesnt exist as I called my database something different. It seems the only way to point EF code first at a different database is by having the a connection string called MyModelContext which obviously doesnt work with MVCProfiler. Any ideas? – Gavin Jul 24 '11 at 07:15
  • Hi Gavin. Can you send your connection string? Obviously without any passwords etc. – Chris Foster Jul 24 '11 at 20:26
  • yes it looks like this Server=sub.host.net;Database=db3262;User ID=xxx;Password=xxx;. The entity framework is picking up the server and username/password but since renaming it, its defaulting the database to a convention of myNamespace.myModel.myContext. I read somewhere that Database.DefaultConnectionFactory will only set the server/user/password but will not set the database. – Gavin Jul 25 '11 at 04:42
  • 1
    Best I can come up with for now is [Changing the Database Name](http://blogs.msdn.com/b/adonet/archive/2010/09/02/ef-feature-ctp4-dbcontext-and-databases.aspx?PageIndex=2). I'd be interested if anyone can come with a better way. – Chris Foster Jul 25 '11 at 11:16
  • 3
    This is not working for me at all.. I had the same issue with naming precedence but even after changing it it is NOT showing ANY SQl profiling info at all – nacho10f Aug 02 '11 at 23:31
  • I was hopefully, but unfortunately I ended up with same problem as @Gavin did. Changing the name of the database is not an option. – RyanW Aug 28 '11 at 03:25
  • I didn't change the name of the database, the link Chris Foster posted tells you how to re-point EF to a different database name. http://blogs.msdn.com/b/adonet/archive/2010/09/02/ef-feature-ctp4-dbcontext-and-databases.aspx?PageIndex=2 – Gavin Aug 29 '11 at 05:33