从 1.7 .NET MVC3 和 EF Code First 升级到 MvcMiniProfiler 1.9
今天我又回到了一个我在过去一个月左右没有参与的项目中。我使用 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.
作为参考,让我向您展示如何使用 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 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?这是正确的吗?
如何使用此模型配置数据库分析?我正在深入挖掘文档,但是旧方法与新方法混合在一起的信息太多,我很难区分当今的“正确”方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
呃,对不起各位。
我解决了这个问题。
我必须在我的数据模型项目中添加 EF MVCMiniProfiler NuGet 包,并将连接类型更改为 EFProfiledConnection,如下所示:
猜猜看,看看 EF 如何分解为它自己的连接类型,这对我来说应该是显而易见的。
我还注释掉了配置设置,以便我可以确认它们不再需要。
我不知道这是否是配置 EF 和 MVC Mini 1.9 的“正确”方法,但它有效。
我愿意接受有关改进的建议,或者是否有更正确的方法来做到这一点,但现在我已经恢复并再次运行。
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:
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.