从1.7 .NET MVC3和EF代码升级到MvcMiniProfiler 1.9 [英] Upgrading to MvcMiniProfiler 1.9 from 1.7 .NET MVC3 and EF Code First

查看:169
本文介绍了从1.7 .NET MVC3和EF代码升级到MvcMiniProfiler 1.9的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今天我已经退回了一个我在过去一个月左右没有一直工作的项目。我使用MiniProfiler 1.7配置了这个项目,并且在世界上都很好。它分析了我的数据库调用和视图性能。



我决定升级到1.9,我遇到了一些速度颠簸。现在,我已经解决了大多数问题。唯一看似错误的是DB分析。我正在丢下一个黄色的屏幕,出现以下错误:

 在调用get_ProviderFactory方法后返回null在一个商店
提供者实例类型'MvcMiniProfiler.Data.ProfiledDbConnection'。
商店提供商可能无法正常运行。

有关参考,请告诉我如何使用MVC3和EF 4.1 Code First在1.7中安装miniprofiler 。



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处理最多的一切从那里。我将列出相关的Application_Start()代码,而不是现在工作的。

  #region MVC Mini Profiler相关数据库profiling config / setup 

//这行使SQL格式化更加智能,所以你可以将
//从分析器直接复制到查询分析器
MiniProfiler.Settings.SqlFormatter = new SqlServerFormatter();

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

#endregion

最后一步是在我的上下文中挂钩分析连接:

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

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

快进到今天,我已经修改了使用MVC3 minprofiler nuget软件包和EF miniprofiler NuGet软件包,但我失去了如何使DB分析工作再次工作。



我修改了我的web.config以下这些似乎是需要的,但是ReSharper不开心。

  < system.data> 
< DbProviderFactories>
< remove invariant =MvcMiniProfiler.Data.ProfiledDbProvider/>
< add name =MvcMiniProfiler.Data.ProfiledDbProviderinvariant =MvcMiniProfiler.Data.ProfiledDbProvider
description =MvcMiniProfiler.Data.ProfiledDbProvider
type =MvcMiniProfiler.Data.ProfiledDbProviderFactory ,MvcMiniProfiler.EntityFramework,Version = 1.9.1.0,Culture = neutral,PublicKeyToken = b44f9351044011a3/>
< / DbProviderFactories>





不太确定我在这里缺少什么我甚至需要这个调用MiniProfilerEF.Initialize();?一些文档和建议似乎表明你甚至不需要这个。



更大的问题是如何在1.9中设置DB Profiling。



我在Global.asax中曾经有过的相关代码现已被移动到App_Start文件夹中的MiniProfiler.cs中。我认为安装程序是一样的,但似乎并非如此。



我想这样做(也许是因为这只是我在1.7中熟悉的)

  // TODO:要配置标准的DbConnection:
var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings [Database]。ConnectionString);
var profiled = new ProfiledDbConnectionFactory(factory);
Database.DefaultConnectionFactory = profiled;

这似乎不再工作了。我也注意到,似乎我现在应该使用EFProfiledDbConnection,而不是仅仅是ProfiledDbConnection?这是否正确?



如何使用此模型配置数据库概要分析?我通过文档挖掘高低,但是有了很多信息,旧的方式与新的方式混合,我很难分开今天的正确方式。

解决方案

呃,对不起家伙。



我解决了这个问题。 b

我必须在我的数据模型项目中添加EF MVCMiniProfiler NuGet包,并将连接类型更改为EFProfiledConnection,如下所示:

  ///< summary> 
///将我们自己的连接字符串提供给DBContext,以便为SQL查询使用mini-profiler。
///< / summary>
///< returns> DBConnection< / returns>
private static DbConnection GetProfilerConnection()
{
返回新的EFProfiledDbConnection(new SqlConnection(ConfigurationManager.ConnectionStrings [Database]。ConnectionString),
MiniProfiler.Current);
}

猜测这应该对我来说很明显,看看EF如何被分解成它是自己的连接类型。



我还评论了配置设置,以便我可以确认不再需要。


$ b $我不知道这是配置EF和MVC Mini 1.9的正确方式,但它是有效的。



我有兴趣提出改进建议,或者如果有更多的正确的方式执行此操作,但现在我已经备份再次运行。


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.

解决方案

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.

这篇关于从1.7 .NET MVC3和EF代码升级到MvcMiniProfiler 1.9的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆