记录实体框架5的SQL语句的数据库第一的aproach [英] Logging SQL statements of Entity Framework 5 for database-first aproach

查看:177
本文介绍了记录实体框架5的SQL语句的数据库第一的aproach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用实体框架跟踪提供程序日志生成的SQL staments。

I'm trying to use Entity Framework Tracing Provider to log the SQL staments generated.

我改变了我的上下文类的东西是这样的:

I changed my context class to something like this:

public partial class MyDBContext: DbContext
{

    public MyDBContext(string nameOrConnectionString)
    : base(EFTracingProviderUtils.CreateTracedEntityConnection(nameOrConnectionString), true)
{
    // enable sql tracing
    ((IObjectContextAdapter) this).ObjectContext.EnableTracing();
}

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }

    //DbSets definition....
}

但是,这并不记录在输出窗口中的SQL语句...

But this doesn't log the SQL statements on the Output window...

我应该有更多的东西在课堂上还是在web.config文件? (我工作的一个ASP.NET MVC 4项目)

Should I had something more in the class or in web.config file? (I'm working on a ASP.NET MVC 4 project)

我使用以下职位的解决方案:实体框架4.1 - EFTracingProvider

I using the solution in the following post:Entity Framework 4.1 - EFTracingProvider

但我做了一些改变,我不知道他们是很重要的:

but I made some changes that I don't know if they are important:

类是局部的,而不是抽象的,和构造函数是公众,而不是保护...

The class is partial instead of abstract, and the constructor is public instead of protected...

我是什么失踪?

推荐答案

修改您的code后,您需要启用在你的web.config跟踪这样的:

After modifying your code, you need to enable tracing in your web.config like this:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.diagnostics>
    <sources>
      <source name="EntityFramework.NorthwindEntities" switchValue="All" />
    </sources>
  </system.diagnostics>
</configuration>

您TraceSource的名称应符合的EntityFrameworkpfixed上下文名称$ P $。
请确保您关闭时通过设置应用程序部署到生产跟踪 switchValue 关闭

The name of your TraceSource should be your context name prefixed with 'EntityFramework'. Make sure that you disable tracing when you deploy your application to production by setting the switchValue to Off.

这篇关于记录实体框架5的SQL语句的数据库第一的aproach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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