MvcMiniProfiler Unable to create an object of type EFProfiledDbConnection

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

My project
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 in 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 get 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 occurs in EF calls

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

If I change the version of MvcMiniProfiler.Data.ProfiledDbProvider in Web.Config from 1.8.0.0 to 1.9.0.0 A new type of exception occurred in the call to 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() 
+6
source share
2 answers

Perhaps this will help. Move MiniProfilerEF.Initialize(); to the beginning of the Application_Start() method. Note that in EF 4.1 and later, the method to be called must be MiniProfilerEF.Initialize_EF42(); .

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

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

Here is 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 objects with an IOC container using this method or, alternatively, use something like

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

Edit: Forgot to add

MiniProfilerEF.Initialize ();

This is used only for the first EF code.

+4
source

All Articles