Upgrading to MvcMiniProfiler 1.9 from 1.7 .NET MVC3 and EF Code First

淺唱寂寞╮ 提交于 2019-12-11 07:16:19

问题


Today I've dropped back into a project that I haven't been working with for the past month or so. I had this project configured using MiniProfiler 1.7 and all was well in the world. It profiled my DB calls and the view performance as well.

I decided to upgrade to 1.9 and I've run into a few speed bumps. Now, I've worked through most of the issues at this point. The only thing that seems "wrong" is DB profiling. I'm getting dropped a yellow screen of death with the following error:

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.

For reference, let me show you how I had miniprofiler setup in 1.7 with MVC3 and EF 4.1 Code First.

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.7.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
</DbProviderFactories>

Global.asax handled most everything from there. I'll list the relevant Application_Start() code that worked prior that doesn't now.

#region MVC Mini Profiler related database profiling config/setup

        //This line makes SQL Formatting smarter so you can copy/paste 
        // from the profiler directly into Query Analyzer
        MiniProfiler.Settings.SqlFormatter = new SqlServerFormatter();

        var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["Database"].ConnectionString);
        var profiled = new ProfiledDbConnectionFactory(factory);
        Database.DefaultConnectionFactory = profiled;

#endregion

With the last step being in my context hooking in the profiled connection:

public class Database : DbContext
{
    public Database()
        : base(GetProfilerConnection(), true)
    {}

    private static DbConnection GetProfilerConnection()
    {
        return
            ProfiledDbConnection.Get(
                new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString));
    }
}

Fast forward to today and I've reworked things to use the MVC3 minprofiler nuget package and the EF miniprofiler NuGet package, but I'm lost on how to get DB profiling working again.

I've modified my web.config to the following which seemed to be what was required but ReSharper isn't happy first off.

  <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.EntityFramework, Version=1.9.1.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
</DbProviderFactories>

Not quite sure what I'm missing here. Do I even need this anymore with a call to MiniProfilerEF.Initialize();? Some documentation and suggestions seem to indicate you don't even need this anymore.

The bigger problem is how to setup DB Profiling in 1.9.

The relevant code that I had prior in Global.asax has now been moved into MiniProfiler.cs in the App_Start folder. I figured the setup would have been the same but that doesn't appear to be the case.

I want to do this (perhaps because this is just what I'm familiar with in 1.7)

//TODO: To profile a standard DbConnection: 
        var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["Database"].ConnectionString);
        var profiled = new ProfiledDbConnectionFactory(factory);
        Database.DefaultConnectionFactory = profiled;

This doesn't seem to work any longer. I've also noted that it seems I should be using EFProfiledDbConnection now instead of just ProfiledDbConnection? Is this correct?

How do I go about configuring DB profiling with this model? I'm digging high and low through documentation, but there's so much information with the old way mixed in with the new way and I'm having a hard time separating what the "correct" way is today.


回答1:


Ugh, sorry guys.

I solved the problem.

I had to add the EF MVCMiniProfiler NuGet package in my data model project and change the connection type there to EFProfiledConnection as so:

        /// <summary>
    /// Supply our own connection string to the DBContext to utilize mini-profiler for SQL queries as well.
    /// </summary>
    /// <returns>DBConnection</returns>
    private static DbConnection GetProfilerConnection()
    {
        return new EFProfiledDbConnection(new SqlConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString),
                                        MiniProfiler.Current);
    }

Guess that should have been obvious to me looking at how EF was broken out into it's own connection type.

I also commented out the config settings so I can confirm that they're no longer required.

I don't know if this is the "right" way of configuring EF and MVC Mini 1.9, but it works.

I'm open to suggestions on improvement or if there's a more correct way of doing this, but for now I'm back up and running again.



来源:https://stackoverflow.com/questions/7666719/upgrading-to-mvcminiprofiler-1-9-from-1-7-net-mvc3-and-ef-code-first

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!