MvcMiniProfiler无法转换类型EFProfiledDbConnection对象 [英] MvcMiniProfiler Unable to cast object of type EFProfiledDbConnection

查看:306
本文介绍了MvcMiniProfiler无法转换类型EFProfiledDbConnection对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想MvcMiniProfiler集成到我的asp.net MVC +实体framewok项目。一切都OK了,第一次Web请求,但它是在其他请求给予例外。



我的项目是

MVC 3

实体框架4.1(DatabaseFirst + POCO发电机的DbContext)

MvcMiniProfiler.dll 1.9.0.0

MvcMiniProfiler.EntityFramework.dll 1.9.1.0

口从安装MvcMiniProfiler怒江-GET



下面添加到Global.asax中

 保护无效的Application_BeginRequest()
{
如果(Request.IsLocal)
{
MvcMiniProfiler.MiniProfiler.Start();
MiniProfilerEF.Initialize();
}
}



添加下面的web.config

 <&System.Data这GT; 
< D​​bProviderFactories>
<清除不变=MvcMiniProfiler.Data.ProfiledDbProvider/>
<添加名称=MvcMiniProfiler.Data.ProfiledDbProvider不变=MvcMiniProfiler.Data.ProfiledDbProvider描述=MvcMiniProfiler.Data.ProfiledDbProviderTYPE =MvcMiniProfiler.Data.ProfiledDbProviderFactory,MvcMiniProfiler,版本= 1.8.0.0文化=中性公钥= b44f9351044011a3/>
< / DbProviderFactories>
< /system.data>



我得到这个例外

  System.InvalidCastException是由用户代码
消息=无法转换类型'MvcMiniProfiler.Data.EFProfiledDbConnection'的对象类型System.Data.SqlClient.SqlConnection未处理。
来源= System.Data这
堆栈跟踪:
在System.Data.SqlClient.SqlCommand.set_DbConnection(的DbConnection值)
在System.Data.Common.DbCommand.set_Connection(价值的DbConnection )
在MvcMiniProfiler.Data.ProfiledDbCommand.set_DbConnection(的DbConnection值)C:\Users\sam\Desktop\mvc-迷你profiler\MvcMiniProfiler\Data\ProfiledDbCommand.cs:118线
在System.Data.Common.DbCommand.set_Connection(的DbConnection值)
在System.Data.Common.Utils.CommandHelper.SetStoreProviderCommandState(EntityCommand entityCommand,EntityTransaction entityTransaction,DbCommand和storeProviderCommand)
。在系统.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(entityCommand entityCommand,行为的CommandBehavior)
在System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute [TResultType](ObjectContext的背景下,ObjectParameterCollection的parameterValues​​)



异常在EF英孚教育发生调用

  ModaEntitiesWrapper.GetInstance()第
.AsNoTracking()
。凡(p => p.StatusId == 1).ToList();

如果我在Web.Config中更改MvcMiniProfiler.Data.ProfiledDbProvider的版本从1.8.0.0到1.9。 0.0
的新类型的异常在MiniProfilerEF.Initialize()调用发生。

  System.IndexOutOfRangeException被用户未处理码
消息=给定的DataRow是不在当前DataRowCollection。
来源= System.Data这
堆栈跟踪:在System.Data.DataRowCollection.Remove(DataRow的行)
在MvcMiniProfiler.MiniProfilerEF.Initialize()


解决方案

也许这将有所帮助。移动 MiniProfilerEF.Initialize(); 来的方法的顶部的Application_Start()。请注意,在EF 4.1及更高版本叫应 MiniProfilerEF.Initialize_EF42()方法; 而不是

 保护无效的Application_Start(){
Logger.Info(应用程序启动);
MiniProfilerEF.Initialize_EF42();
// ...
}


I am trying to integrate MvcMiniProfiler to my asp.net mvc+entity framewok project. Everything is ok for the first time web request but it is giving exception at the other requests.

My Project is
MVC 3
Entity Framework 4.1 (DatabaseFirst + POCO Generator DbContext)
MvcMiniProfiler.dll 1.9.0.0
MvcMiniProfiler.EntityFramework.dll 1.9.1.0
I install MvcMiniProfiler from Nu-Get

Added below to global.asax

    protected void Application_BeginRequest()
    {
        if (Request.IsLocal)
        {
            MvcMiniProfiler.MiniProfiler.Start();
            MiniProfilerEF.Initialize();       
        }
    }

Added below to 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>

I am getting this 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)


Exception is occured in EF calls

ModaEntitiesWrapper.GetInstance().Articles
               .AsNoTracking()
               .Where(p => p.StatusId == 1).ToList();

If I change version of MvcMiniProfiler.Data.ProfiledDbProvider in Web.Config from 1.8.0.0 to 1.9.0.0 A new type of exception occured in MiniProfilerEF.Initialize() call.

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

解决方案

Maybe this will help. Move the MiniProfilerEF.Initialize(); to the top of the method Application_Start(). Note that in EF 4.1 and higher the method called should be MiniProfilerEF.Initialize_EF42(); instead.

protected void Application_Start() {
    Logger.Info("Application start");
    MiniProfilerEF.Initialize_EF42();
    // ...
}

这篇关于MvcMiniProfiler无法转换类型EFProfiledDbConnection对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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