9

I have a asp.net project that uses mvc-mini-profiler. I was using version 1.7 of the NuGet package and I noticed that there is an updated package whose version is 1.9. I updated the package and now my code no longer compiles. The code that fails to compile is:

public static T GetProfiledContext<T>() where T : System.Data.Objects.ObjectContext
{
    var conn = GetStoreConnection<T>();
    if (_enableProfiling)
    {
        conn = ProfiledDbConnection.Get(conn);
    }
    return ObjectContextUtils.CreateObjectContext<T>(conn);
}

The compilation errors report the following issues:

  • 'MvcMiniProfiler.Data.ProfiledDbConnection' does not contain a definition for 'Get'.
  • The name 'ObjectContextUtils' does not exist in the current context.

I noticed that I can create an instance of ProfiledDbConnection and pass it the connection and an object of type IDbProfiler, but I am not sure how I should obtain that object.

Regarding ObjectContextUtils, I have no clue of what I am supposed to use.

How can I fix these issues?


Update:

By following @monkeychatter's recommendations, I managed to build the code. I now get the following runtime exception:

A null was returned after calling the 'get_ProviderFactory' method on a store provider instance of type 'MvcMiniProfiler.Data.ProfiledDbConnection'. The store provider might not be functioning correctly.

By inspecting ProfiledDbConnection in ILSpy, I noticed that it no longer overrides the DbProviderFactory. That seems to be the cause of the error, since the base implementation returns null. Has anybody been able to work around this issue?

Antoine Aubry
  • 12,203
  • 10
  • 45
  • 74

1 Answers1

12

I just went through the same and unfortunately most/all documentation shows the 'old' way. The majority of this functionality has been moved to the ProfiledDbConnection class itself. To get the ObjectContext extension on ProfiledDbConnection you also need to reference an assembly from the nuget package 'MiniProfiler.EF'. Below are the edits to get the equivalent code in 1.9.

//reference extension from MvcMiniProfiler.Data
using MvcMiniProfiler.Data;

var conn = GetStoreConnection<T>();   
if (_enableProfiling)   
{   
    //conn = ProfiledDbConnection.Get(conn);   
    conn = new ProfiledDbConnection(conn, MiniProfiler.Current);
}   
//return ObjectContextUtils.CreateObjectContext<T>(conn);
return conn.CreateObjectContext<T>();

Update: Per your updated question I would replace the line in my previous solution as below. This includes an override to fix up the ProviderFactory issue:

    //conn = new ProfiledDbConnection(conn, MiniProfiler.Current);
    conn = new EFProfiledDbConnection(conn, MiniProfiler.Current);
tonysurma
  • 136
  • 1
  • 4
  • I can't find a CreateObjectContext method on ProfiledDbConnection. Are you sure that the above code is correct? – Antoine Aubry Aug 31 '11 at 14:17
  • Good point, thanks. I forgot to mention that comes from an add-on nuget package MiniProfiler.EF that includes the assembly MvcMiniProfiler.EntityFramework. Answer edited to reflect that. – tonysurma Aug 31 '11 at 14:26
  • Thanks, that helped. However, I still have problems. I have updated my question with more information. – Antoine Aubry Aug 31 '11 at 14:47
  • I had that issue too :) I will dig up my code I had to use to fix it but the basic issue is that it isn't finding your 'MetadataWorkspace' which it then uses some fancy reflection on to set the providerfactory. After that it then creates an EntityConnection (using the metadataworkspace and the now profiled connection as ctor args). That EntityConnection is then the parameter to the ObjectContext that it creates and ultimately returns. – tonysurma Aug 31 '11 at 15:34
  • Awesome! Now I am getting "Unable to determine the provider name for connection of type 'MvcMiniProfiler.Data.EFProfiledDbConnection'.". I guess it is related to the providers in my web.config. Dis you encounter this issue? Thanks – Antoine Aubry Aug 31 '11 at 17:39
  • 4
    Did you find a solution to the "Unable to determine the provider name ... " exception? – Charlino Sep 01 '11 at 03:16
  • Not yet. I am still searching for it. I accepted the answer because it answered my original question. – Antoine Aubry Sep 01 '11 at 09:42
  • I have asked a new question regarding this issue. If you know the answer, please answer there. Thanks. http://stackoverflow.com/questions/7286481/unable-to-determine-the-provider-name-error-with-mvc-mini-profiler-1-9 – Antoine Aubry Sep 02 '11 at 16:29