asp.net-mvc - 使用 MvcMiniProfiler 分析 Entity Framework

标签 asp.net-mvc asp.net-mvc-3 entity-framework mvc-mini-profiler

我有一个 Asp.net Mvc 3 应用程序,它现在正在使用 MvcMiniProfiler。我还使用 Entity Framework 来访问我的数据库,并且我想让探查器能够使用实体模型。到目前为止,我已经在下面创建了上下文工厂:

internal class ProfiledContextFactory : IContextFactory
{
    public ModelContainer GetContext()
    {
        var conn = ProfiledDbConnection.Get(GetConnection());
        return ObjectContextUtils.CreateObjectContext<ModelContainer>(conn);
    }

    private static EntityConnection GetConnection()
    {
        return new EntityConnection(ConfigurationManager.ConnectionStrings["ModelContainer"].ConnectionString);
    }
}

当我运行上面的代码(当我开始一个工作单元时由我的存储库层调用)时,它在调用 MvcMiniProfiler.ProfiledDbServices 类中的 CreateDbCommandDefinition 时陷入无限循环。

有什么线索是我做错了吗?

最佳答案

问题是我的 GetConnection 返回的是 EntityConnection,而不是 EntityConnection 中的 SqlConnection。我现在修改了我的代码,使其显示为:

private static SqlConnection GetConnection()
{
    var connStr = ConfigurationManager.ConnectionStrings["ModelContainer"].ConnectionString;
    var entityConnStr = new EntityConnectionStringBuilder(connStr);
    return new SqlConnection(entityConnStr.ProviderConnectionString);
}

而且效果很好。

我在看这个问题时发现了这一点:Using mvc-mini-profiler with EF 4.0 and Ninject

关于asp.net-mvc - 使用 MvcMiniProfiler 分析 Entity Framework ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6443398/

相关文章:

.net - ajax 调用时浏览器卡住

c# - ASP.NET MVC 表单发布

jquery - ASP MVC JsonResult 呈现为页面,而不是通过调用 jQuery 函数捕获

c# - 根据属性值映射到类

nhibernate - 说服顽固的DBA将ORM用于大多数CRUD与存储过程, View 和函数

asp.net-mvc - asp.net mvc 数据注释验证url

c# - Asp.net MVC View 中的延迟执行是一件非常糟糕的事情吗?

c# - 在 ASP.NET MVC 中什么时候使用 ViewBag/ViewData 是 "acceptable"?

c# - 如何在 Razor 中定义方法?

c# - 在单元测试中连接到 localDb 会抛出异常