EF 4.1上的Mvc-Mini-Profiler v1.7 Code-First项目并不配置SQL [英] Mvc-Mini-Profiler v1.7 on EF 4.1 Code-First project doesn't profile SQL

查看:198
本文介绍了EF 4.1上的Mvc-Mini-Profiler v1.7 Code-First项目并不配置SQL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我昨天在我的项目中设置了MiniProfiler.MVC3 - 1.7包。控制器和视图分析工作正常,但我真正感兴趣的是SQL Profiling。我没有能够让这个工作。我使用EF Code First与SQL 2008数据库。

I setup MiniProfiler.MVC3 - 1.7 package in my project yesterday. The Controller and view profiling is working fine, but the peice I'm really interested in is the SQL Profiling. I have not been able to get this to work. I'm using EF Code First with a SQL 2008 database.

我已经遵循了这篇文章中的所有建议。

I have followed all the suggestions in this post ..

mvcminiprofiler- on-ef-4-1-code-first-project-doesnt-profile-sql

在miniprofiler.cs中,我的SQL连接设置为...

In the miniprofiler.cs i have my SQL connection setup as...

 var factory = new SqlConnectionFactory(ConfigurationManager.ConnectionStrings["CMDBMVC3"].ConnectionString);

我的Web.config数据库连接是...

My Web.config db connection is...

 <add name="CMDBMVC3" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI; AttachDBFilename=|DataDirectory|CMDB_MVC3.mdf;Initial Catalog=CMDB_MVC3;Trusted_Connection=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />

如果我在微型分析器行上放置一个断点,它指向正确的数据库连接。我不知道在这一点上还有什么要做的。我会感谢任何关于如何获取SQL分析工作的方向。

If I put a breakpoint on the mini-profiler line it points to the correct db connection. I'm not sure what else to do at this point. I would appreciate any direction on how to get the SQL profiling working.

推荐答案

我使用EF代码,并在我的上下文构造函数我创建一个新的连接工厂并将其传递给ProfiledDbConnectionFactory方法,该方法返回一个配置式连接,然后您可以将其设置为上下文的DefaultConnectionFactory。

I use EF Code first and the mini profiler within my Context constructor I create a new connection factory and pass this to the ProfiledDbConnectionFactory method this returns a profiled connection that you can then set as the DefaultConnectionFactory of the context.

public MyConext()
{
    var factory = new ConnectionFactory(); 
    var profiled = new MvcMiniProfiler.Data.ProfiledDbConnectionFactory(factory);
    Database.DefaultConnectionFactory = profiled;
}

连接Facotry刚刚返回一个新的sql连接

The connection Facotry just returns a new sql connection

public class ConnectionFactory :IDbConnectionFactory
{
     public DbConnection CreateConnection()
     {
         var cnn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["SomeConnection"].ToString());

             return cnn;            
    }

您还需要将ProfiledDBProvider添加到Web配置文件中。确保版本号是正确的。

You also need to add the ProfiledDBProvider to the web config file. Make sure the version number is correct for you.

<system.data>
    <DbProviderFactories>
      <remove invariant="MvcMiniProfiler.Data.ProfiledDbProvider" />
      <add name="MvcMiniProfiler.Data.ProfiledDbProvider" invariant="MvcMiniProfiler.Data.ProfiledDbProvider"
              description="MvcMiniProfiler.Data.ProfiledDbProvider"
              type="MvcMiniProfiler.Data.ProfiledDbProviderFactory, MvcMiniProfiler, Version=1.7.0.0, Culture=neutral, PublicKeyToken=b44f9351044011a3" />
    </DbProviderFactories>
  </system.data>

这对于使用Miniprofiler nuget包的MVC和asp.net webforms都适用于我。我还会查看新的MVC版本的nuget软件包,该软件可以自动将配置文件配置为全局操作过滤器的一部分。

This works fine for me in both MVC and asp.net webforms using the Miniprofiler nuget package. I'd also check out the new MVC version of the nuget package that auto configs profiling as part of a global action filter.

这篇关于EF 4.1上的Mvc-Mini-Profiler v1.7 Code-First项目并不配置SQL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆