0

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....
}

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

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

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...

What am I missing?

Community
  • 1
  • 1
amp
  • 11,754
  • 18
  • 77
  • 133

1 Answers1

3

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>

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.

Wouter de Kort
  • 39,090
  • 12
  • 84
  • 103
  • This approach can be applied in code-first approach am I right? – amp Apr 05 '13 at 09:33
  • 1
    Yep should be possible. Are you have any problems? – Wouter de Kort Apr 05 '13 at 09:36
  • Until now, no problems with the database-first option. I was just asking to be sure if this method can be applied in others approaches. Thanks again! – amp Apr 05 '13 at 09:47
  • If you are going to try it in Code First be sure to read the post you linked to again. You need some extra code for the creation of the tracing connection. – Wouter de Kort Apr 05 '13 at 09:50
  • Doesn't seem to work with myDbContext.Database.SqlQuery, EFTracingProvider blows up with not supported exception. – root Apr 26 '13 at 16:39