【问题标题】:how to use mvc-mini-profiler for database-first entity framework?如何将 mvc-mini-profiler 用于数据库优先实体框架?
【发布时间】:2012-06-21 05:24:26
【问题描述】:

我正在尝试将 mvc-mini-profiler 用于 db-first EF,但它无法正常工作。

(注意我使用的是objectcontext,而不是dbcontext)

这是我尝试过的 stackoverflow 列表:

版本:

  • 实体框架:4.3.1
  • MiniProfiler:2.0.2
  • MiniProfiler.ef: 2.0.3

这就是我设置 miniprofiler 的方式:

  1. 我在 Global.asax 中添加了以下内容

    protected void Application_BeginRequest(
    {
        MiniProfiler.Start();   
    }
    
    protected void Application_EndRequest()
    {
        MiniProfiler.Stop();
    }
    
    protected void Application_Start()
    {
        ...
    
        MiniProfilerEF.Initialize_EF42();
    }
    
  2. 然后配置一个objectcontext,

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

如果我执行这个,运行“context.MyEntities.ToList()”时会出现以下异常

[System.Data.EntityCommandCompliationException]

内部异常中的消息说: EntityClient 不能用于从存储命令树创建命令定义。

我配置错了吗?有什么帮助吗?

谢谢,

【问题讨论】:

标签: entity-framework mvc-mini-profiler database-first


【解决方案1】:

我使用 MiniProfiler 和数据库优先的实体框架,它运行良好。您可能需要根据以下相关答案在数据库上下文中关闭数据库初始化策略:https://stackoverflow.com/a/9762989/325727

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

参数null 通过确保没有可用的初始化程序来关闭数据库初始化。

【讨论】:

  • 谢谢,但我使用的是 ObjectContext 而不是 DbContext。似乎 Database.SetInitializer 仅适用于 DbContext。
猜你喜欢
  • 2011-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多