MvcMiniProfiler无法转换EFProfiledDbConnection类型的对象

时间:2012-02-18 22:30:42

标签: c# entity-framework mvc-mini-profiler

我正在尝试将MvcMiniProfiler集成到我的asp.net mvc +实体framewok项目中。第一次网络请求时一切正常,但是在其他请求中给出了例外。

我的项目是 MVC 3
实体框架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>

我收到此异常

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

2 个答案:

答案 0 :(得分:6)

也许这会有所帮助。将MiniProfilerEF.Initialize();移至方法Application_Start()的顶部。请注意,在EF 4.1及更高版本中,调用的方法应为MiniProfilerEF.Initialize_EF42();

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

答案 1 :(得分:4)

查看http://code.google.com/p/mvc-mini-profiler/

以下是如何将mvc-mini-profiler与EF Database First一起使用的示例:

public static class Entities
{
    public static MyEntities Create()
    {
         var builder = new EntityConnectionStringBuilder(ConfigurationManager.ConnectionStrings["MyEntities"].ConnectionString);
         var sqlConnection = new SqlConnection(builder.ProviderConnectionString);
         var profiledConnection = new EFProfiledDbConnection(sqlConnection, MiniProfiler.Current);

         return profiledConnection.CreateObjectContext<MyEntities>();
    }
}

然后,您可以使用该方法向IOC容器注册您的实体,或者使用类似

的内容
using(var entities = Entities.Create())
{
    //Do stuff here
    entities.SaveChanges();
}

编辑:忘记添加

  

MiniProfilerEF.Initialize();

仅用于EF Code First。