Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC MiniProfiler with Entity Framework 5 Model First Error: Unable to determine the provider name

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.

like image 293
Yaakov Ellis Avatar asked Dec 19 '12 19:12

Yaakov Ellis


1 Answers

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);
like image 51
amhed Avatar answered Sep 30 '22 15:09

amhed