14

Using Entity Framework 5, MiniProfiler 2. Nuget installed for MiniProfiler.EF.

Creating a connection as follows

var conn = new EFProfiledDbConnection(DbConnections.GetSqlConnection(),                                                                      MiniProfiler.Current);
return new MyDbContext(conn, true);

When trying to retrieve data using the DbContext, the following error is returned:

Unable to determine the provider name for connection of type     
'StackExchange.Profiling.Data.EFProfiledDbConnection'.`

I have tried adding the following to web.config:

<system.data>
  <DbProviderFactories>
    <remove invariant="StackExchange.Profiling.Data.ProfiledDbProviderFactory" />
    <add name="StackExchange.Profiling.Data.ProfiledDbProviderFactory" 
         invariant="StackExchange.Profiling.Data.ProfiledDbProviderFactory" 
         description="StackExchange.Profiling.Data.ProfiledDbProviderFactory"
         type="StackExchange.Profiling.Data.ProfiledDbProviderFactory, MiniProfiler, Version=2.0.2.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
  </DbProviderFactories>
</system.data>

That doesn't help. I have also tried variations on this, using the different EFProviderFactories in the MiniProfiler.EntityFramework, but couldn't get any of them to work.

If I try running MiniProfilerEF.Initialize(); in App_Start, then I get the following error when trying to access the DB:

Code generated using the T4 templates for Database First and Model First development may not work correctly if used in Code First mode. To continue using Database First or Model First ensure that the Entity Framework connection string is specified in the config file of executing application. To use these classes, that were generated from Database First or Model First, with Code First add any additional configuration using attributes or the DbModelBuilder API and then remove the code that throws this exception.

Removing the DbProviderFactories section from web.config and running MiniProfilerEF.Initialize_EF42(); in App_Start results in the original error.

As the MiniProfiler page says that MiniProfilerEF.Initialize() is only for code first, this seems like it is not the way to go anyway.

Other searching hasn't given me any other things to try, other than adding in the section to Web.Config. Any recommendations on how to proceed? Goal is to be able to use Model First DbContext profiled by Mvc MiniProfiler.

Yaakov Ellis
  • 40,752
  • 27
  • 129
  • 174
  • 3
    Are you sure it supports `EF5`? http://community.miniprofiler.com/permalinks/99/sqlexception-on-ef-5-w-net-4-5 – MisterIsaak Dec 19 '12 at 20:08
  • Did you look at http://stackoverflow.com/questions/6785758/mvc-mini-profiler-configuration-clarification? I'm using EF5 code first, but I think you might be wrong at line 2 when directly returning new context. – berezovskyi Jan 26 '13 at 19:22
  • @ABerezovskiy no, that solution doesn't work - the `ProfiledDbConnection.CreateObjectContext()` function expects `T` to be of type `ObjectContext`, when in my case it is of type `DbContext`. So it doesn't compile. – Yaakov Ellis Jan 27 '13 at 19:06
  • @YaakovEllis `((IObjectContextAdapter)yourDbContextInstance).ObjectContext` Lerman J. - Programming Entity Framework: DbContext, p.204, 2012 – berezovskyi Jan 27 '13 at 19:36
  • @ABerezovskiy I don't have a `DbContextInstance` yet to use in this way - I am trying to create and return the `DbContextInstance` using the `ProfiledDbConnection` in order to integrate with MiniProfiler – Yaakov Ellis Jan 27 '13 at 19:56
  • 1
    Any luck on this? Spent so much time trying to figure this issue out. Help Would be much appreciated!!! – TYRONEMICHAEL Feb 16 '13 at 22:41

1 Answers1

8

Does your global.asax look like this?

public class MvcApplication : HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();

        WebApiConfig.Register(GlobalConfiguration.Configuration);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
        AuthConfig.RegisterAuth();
        AutomapperConfig.RegisterMappings();

        MiniProfiler.Settings.SqlFormatter = new StackExchange.Profiling.SqlFormatters.SqlServerFormatter();
        MiniProfilerEF.Initialize();
    }

    protected void Application_BeginRequest()
    {
        if (Request.IsLocal)
        {
            MiniProfiler.Start();
        }
    }

    protected void Application_EndRequest(object sender, EventArgs e)
    {
        MiniProfiler.Stop(false);
    }
}

I instantiate my context the regular way:

var _database = new DinoContext(_connectionString);
amhed
  • 3,649
  • 2
  • 31
  • 56
  • Thanks, I had the same error and was just missing the `MiniProfilerEF.Initialize();` statement. – mono68 Oct 17 '13 at 07:27