1

今天我又回到了一个我过去一个月左右没有参与的项目。我使用 MiniProfiler 1.7 配置了这个项目,一切都很好。它还分析了我的数据库调用和视图性能。

我决定升级到 1.9,但遇到了一些减速带。现在,我已经解决了此时的大部分问题。唯一看起来“错误”的是数据库分析。我因以下错误而出现黄屏死机:

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.

作为参考,让我向您展示我如何在 1.7 中使用 MVC3 和 EF 4.1 Code First 设置 miniprofiler。

网络配置

  <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 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

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

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

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

快进到今天,我重新设计了一些东西以使用 MVC3 minprofiler nuget 包和 EF miniprofiler NuGet 包,但我不知道如何让数据库分析再次工作。

我已将我的 web.config 修改为以下内容,这似乎是必需的,但 ReSharper 一开始并不高兴。

  <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>

在此处输入图像描述

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

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

我之前在 Global.asax 中的相关代码现在已移至 App_Start 文件夹中的 MiniProfiler.cs 中。我认为设置将是相同的,但似乎并非如此。

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

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

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

如何使用此模型配置数据库分析?我正在通过文档从高处和低处挖掘,但是旧方式与新方式混合在一起的信息太多了,我很难区分今天的“正确”方式是什么。

4

1 回答 1

2

呃,对不起各位。

我解决了这个问题。

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

        /// <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);
    }

猜想这对我来说应该是显而易见的,看看 EF 是如何分解成它自己的连接类型的。

我还注释掉了配置设置,因此我可以确认它们不再需要。

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

我愿意接受有关改进的建议,或者是否有更正确的方法来做到这一点,但现在我已经重新启动并再次运行。

于 2011-10-05T19:51:16.617 回答