Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Still problems with profiling EF Database First

Using EF 4.1 and Mini-profiler 1.7. Using model-first, scanned from existing database. EF generates a class that derives from ObjectContext/ObjectSet, not DbContext/DbSet. I couldn't find anywhere to control that.

I tried the popular solutions, to no avail.

Suffering from frustration, I also tried to directly create my Context with an explicit EntityConnection, which was created directly with the ProfiledDbConnection. I wanted to bypass any chance that the connection was not the intended type.

public HomeController() {
try {
    string[] paths = new string[] { @"res://*/" };
    Assembly[] assys = new Assembly[] { Assembly.GetExecutingAssembly() };
    MetadataWorkspace mw = new MetadataWorkspace(paths, assys);
    string cnx = WebConfigurationManager.ConnectionStrings["XXXX"].ConnectionString;
    DbConnection cx = MvcMiniProfiler.Data.ProfiledDbConnection.Get(new SqlConnection(cnx), MiniProfiler.Current);
    //DbConnection cx = Database.DefaultConnectionFactory.CreateConnection(cnx);
    EntityConnection ec = new EntityConnection(mw, cx);
    db = new MyContextEntities(ec);
}
catch (Exception ex) {
    Trace.WriteLine("EDM failed: " + ex.Message);
    db = new MyContextEntities();
}
}

I have verified that the correct path is taken. However, when actually running a LINQ query, we get an exception:

Unable to cast object of type 'MvcMiniProfiler.Data.ProfiledDbConnection' to type 'System.Data.SqlClient.SqlConnection'.

The offending statement is:

return query.ToList();

The stack trace is even more interesting, because apparently something inside EF absolutely wants a SqlConnection!

at System.Data.SqlClient.SqlCommand.set_DbConnection(DbConnection value) 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) at System.Data.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) at System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable.GetEnumerator() at System.Collections.Generic.List1..ctor(IEnumerable1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)

Obviously, if I feed it a SqlConnection instead, everything is happy.

What is going on here? How did this ever work? Maybe it never worked for EDMX case? Does the fact that it is ObjectContext-derived have any bearing?

like image 869
escape-llc Avatar asked Aug 11 '11 16:08

escape-llc


1 Answers

After upgrading EF (4.1.10715.0) and MiniProfiler (1.9.1) packages, and adding MiniProfiler.EF (1.9.1) package, go into the App_Start module (MiniProfiler.cs) and put the following, instead of the ProfiledDbConnectionFactory code:

MiniProfilerEF.Initialize();

And all is well!

like image 51
escape-llc Avatar answered Nov 09 '22 14:11

escape-llc