Search code examples
entity-frameworkmvc-mini-profilerdatabase-first

how to use mvc-mini-profiler for database-first entity framework?


I am trying to use mvc-mini-profiler for db-first EF, but it is not working properly.

(note that I'm using objectcontext, not dbcontext)

Here is the list of stackoverflows I've tried:

versions:

  • Entity Framework: 4.3.1
  • MiniProfiler: 2.0.2
  • MiniProfiler.ef: 2.0.3

This is how I setup miniprofiler:

  1. I've added the following stuff in Global.asax

    protected void Application_BeginRequest(
    {
        MiniProfiler.Start();   
    }
    
    protected void Application_EndRequest()
    {
        MiniProfiler.Stop();
    }
    
    protected void Application_Start()
    {
        ...
    
        MiniProfilerEF.Initialize_EF42();
    }
    
  2. Then configure an objectcontext,

    var entityConnection = new EntityConnection(ConnectionString);
    var profiledDbConnection = new EFProfiledDbConnection(entityConnection, MiniProfiler.Current);
    var context = profiledDbConnection.CreateObjectContext<MyContext>();
    var list = context.MyEntities.ToList();
    

If I execute this, the following exception occurs when running "context.MyEntities.ToList()"

[System.Data.EntityCommandCompliationException]

the message in the inner exception says: EntityClient cannot be used to create a command definition from a store command tree.

Have I configured wrong? Any help?

thanks,


Solution

  • I use MiniProfiler and database first Entity Framework and it does work well. You may need to turn off the database initialization strategy inside of your database context as per this related answer: https://stackoverflow.com/a/9762989/325727

    public class EmployeeContext : DbContext
    {
        static EmployeeContext() { Database.SetInitializer<EmployeeContext>(null); }    
        public IDbSet<Employee> Employees { get; set; } 
    }
    

    The parameter null turns off database initialization by making sure that there is no initializer available.