Still Problems with EF First Database Profiling

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

I tried popular solutions, but to no avail.

In frustration, I also tried to directly create my Context with an explicit EntityConnection, which was created directly with ProfiledDbConnection. I wanted to get around any chance that the join 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 checked the correct path. However, when the LINQ query is actually executed, we get an exception:

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

Alleged violation:

 return query.ToList(); 

Stack tracing is even more interesting, because obviously something inside EF absolutely needs SqlConnection!

in System.Data.SqlClient.SqlCommand.set_DbConnection (value DbConnection) in System.Data.Common.DbCommand.set_Connection (value DbConnection) in System.Data.Common.Utils.CommandHelper.SetStoreProviderCommandState (EntityCommand entityCommand, EntityTransaction entityTransaction, DbCommand storeProviderCommand) in System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands (EntityCommand entityCommand, CommandBehavior behavior) in System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute [TResultType] in ObjectContext 1.GetResults(Nullable 1 forMergeOption) in System.Data.Objects.ObjectQuery 1.System.Collections.Generic.IEnumerable.GetEnumerator() at System.Collections.Generic.List 1..ctor (IEnumerable 1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable 1 source)

Obviously, if I feed him SqlConnection, everything will be happy.

What's going on here? How did it ever work? Maybe he never worked on an EDMX case? Does the fact that it is a derivative of ObjectContext have any meaning?

+6
source share
1 answer

After updating the EF (4.1.10715.0) and MiniProfiler (1.9.1) packages and adding the MiniProfiler.EF (1.9.1) package, go to the App_Start module (MiniProfiler.cs) and set the code instead of ProfiledDbConnectionFactory :

 MiniProfilerEF.Initialize(); 

And everything's good!

+1
source

Source: https://habr.com/ru/post/894857/


All Articles