Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
702 views
in Technique[技术] by (71.8m points)

asp.net mvc - Logging SQL statements of Entity Framework 5 for database-first aproach

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...