我正在尝试将MvcMiniProfiler集成到我的asp.net mvc + entity framewok项目中。第一次网络请求一切正常,但是其他请求异常(exception)。
我的项目是
MVC 3
Entity Framework 4.1(DatabaseFirst + POCO Generator DbContext)
MvcMiniProfiler.dll 1.9.0.0
MvcMiniProfiler.EntityFramework.dll 1.9.1.0
我从Nu-Get安装MvcMiniProfiler
下面添加到global.asax
protected void Application_BeginRequest()
{
if (Request.IsLocal)
{
MvcMiniProfiler.MiniProfiler.Start();
MiniProfilerEF.Initialize();
}
}
在下面添加到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.8.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
</DbProviderFactories>
</system.data>
我收到这个异常(exception)
System.InvalidCastException was unhandled by user code
Message=Unable to cast object of type 'MvcMiniProfiler.Data.EFProfiledDbConnection' to type 'System.Data.SqlClient.SqlConnection'.
Source=System.Data
StackTrace:
at System.Data.SqlClient.SqlCommand.set_DbConnection(DbConnection value)
at System.Data.Common.DbCommand.set_Connection(DbConnection value)
at MvcMiniProfiler.Data.ProfiledDbCommand.set_DbConnection(DbConnection value) in C:\Users\sam\Desktop\mvc-mini-profiler\MvcMiniProfiler\Data\ProfiledDbCommand.cs:line 118
at System.Data.Common.DbCommand.set_Connection(DbConnection value)
at System.Data.Common.Utils.CommandHelper.SetStoreProviderCommandState(EntityCommand entityCommand, EntityTransaction entityTransaction, DbCommand storeProviderCommand)
at System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)
at System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)
EF调用发生异常
ModaEntitiesWrapper.GetInstance().Articles
.AsNoTracking()
.Where(p => p.StatusId == 1).ToList();
如果我将Web.Config中的MvcMiniProfiler.Data.ProfiledDbProvider版本从1.8.0.0更改为1.9.0.0
MiniProfilerEF.Initialize()调用中发生了一种新型的异常。
System.IndexOutOfRangeException was unhandled by user code
Message=The given DataRow is not in the current DataRowCollection.
Source=System.Data
StackTrace:
at System.Data.DataRowCollection.Remove(DataRow row)
at MvcMiniProfiler.MiniProfilerEF.Initialize()
最佳答案
也许这会有所帮助。将MiniProfilerEF.Initialize();
移到Application_Start()
方法的顶部。请注意,在EF 4.1及更高版本中,调用的方法应改为MiniProfilerEF.Initialize_EF42();
。
protected void Application_Start() {
Logger.Info("Application start");
MiniProfilerEF.Initialize_EF42();
// ...
}
关于c# - MvcMiniProfiler无法转换EFProfiledDbConnection类型的对象,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9344777/