11

I'm trying to get MiniProfiler to profile my database access but I'm running into problems. All the help I see out there seems to be for "code first" entity framework connections. My model was designed before the code first update was available this year and I used the designer to create the edmx model. (I've been using this for almost a year and it seems to be working for me)

The example on the MiniProfiler documentation site doesn't make sense to me. I've tried a few variations of it but I'm having problems.

My Model is called CYEntities, normally to instantiate an ObjectContext I just do this var context = new CYEntities() here's what I've tried for the profiler...

var dbConnection = new CYEntities().Connection;
var profiledConnection = ProfiledDbConnection.Get(dbConnection);
var context = profiledConnection.CreateObjectContext<CYEntities>(); // this is the context I'd finally use to access data. 

This throws an exception...

System.ArgumentException: Unable to find the requested .Net Framework Data Provider. It may not be installed.

I'm not sure where to go from here.

BZink
  • 7,687
  • 10
  • 37
  • 55

2 Answers2

11

Try like this:

var connectionString = ConfigurationManager
    .ConnectionStrings["MyConnectionString"]
    .ConnectionString;
var ecsb = new EntityConnectionStringBuilder(connectionString);
var sqlConn = new SqlConnection(ecsb.ProviderConnectionString);
var pConn = ProfiledDbConnection.Get(sqlConn, MiniProfiler.Current);
var context = ObjectContextUtils.CreateObjectContext<CYEntities>(pConn);
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • I'm still getting the ArgumentException on CreateObjectContext. I don't know about data providers. Do I need something in my web.config? Is MiniProfiler giving me a new data provider? – BZink Jul 24 '11 at 14:59
  • @BZink, in your web config you just need the connection string. – Darin Dimitrov Jul 24 '11 at 15:02
  • Yeah. I've gotten that far. ObjectContextUtils.CreateObjectContext throws an exception saying 'Unable to find the requested .Net Framework Data Provider. It may not be installed.' – BZink Jul 24 '11 at 16:53
  • Worked like a charm for me with some additional info from [this answer](http://stackoverflow.com/questions/7257271/mini-profiler-upgrade-from-1-7-to-1-9-breaks-existing-code) – scheien Nov 07 '12 at 12:45
7

I'm having the same issue. It appears that you must add the following to your web.config, however, for me this causes the w3wp.exe process to crash (and the web server in visual studio, for local requests). It doesn't seem like something that is happening to anyone else, so it might work for you (make sure to insert the actual version of the profiler you are using).

<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>
Morten Christiansen
  • 19,002
  • 22
  • 69
  • 94
  • The code for this error is c00000fd which indicates that it is a stack overflow exception. – Morten Christiansen Jul 25 '11 at 12:29
  • Hmm, that got me a little closer I think but I also got the overflow exception and w3wp.exe crash. Debugger said it was in mscorlib.dll. Any chance you're working in an Azure project? – BZink Jul 25 '11 at 14:10
  • I got it working. I added your system.data section to web.config but was getting the crashing w3wp.exe. I was referencing the wrong version of system.data.entity in system.web->compliation->assemblies. I changed... "System.Data.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" to System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 and it's working! – BZink Jul 25 '11 at 14:51
  • No Azure. Strange, I already have it at 4.0.0.0, so that's not it for me. I think I'll try it out on a new MVC project and see how it goes. – Morten Christiansen Jul 26 '11 at 06:28
  • 2
    I can still produce the overflow error (with 4.0.0.0) if I use the method creating the context that I included in my original question. If I move to something closer to Darin's answer it works. It's worth figuring out, the DB profiling is awesome! – BZink Jul 26 '11 at 14:10
  • 1
    Worked for me once I realized that MvcMiniProfiler is now (currently) version 1.7.0.0 not 1.6.0.0 – Greg Jul 27 '11 at 12:51
  • Could you elaborate what you mean by "Something Closer"? – Michiel Cornille Dec 17 '14 at 11:24