MvcMiniProfiler 无法转换 EFProfiledDbConnection 类型的对象

发布于 2025-01-06 17:58:34 字数 2840 浏览 0 评论 0原文

我正在尝试将 MvcMiniProfiler 集成到我的 asp.net mvc+entityframewok 项目中。第一次网络请求一切正常,但在其他请求时出现异常。

我的项目是
MVC 3
实体框架 4.1(DatabaseFirst + POCO 生成器 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() 

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

今天小雨转甜 2025-01-13 17:58:34

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

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

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();
    // ...
}
无边思念无边月 2025-01-13 17:58:34

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

下面是如何将 mvc-mini-profiler 与 EF 数据库首先结合使用的示例:

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。

Check out http://code.google.com/p/mvc-mini-profiler/

Here's an example of how to use mvc-mini-profiler with 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>();
    }
}

You can then register your entities with an IOC container using the method or alternatively use something like

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

Edit: Forgot to add

MiniProfilerEF.Initialize();

That is only used for EF Code First.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文