Friday, August 26, 2011

EF Code-First in the shape of NHibernate - Profiling



ORM tools made datacentric programs too easy to develop, but if it's been used without care, it will soon face a disaster in terms of SQL efficiency.
Microsoft proposes a tool for the developers who wants to know what is happening under the hood in EF.
It means, all SQL statements and information can be traced and logged in a file based on the developer's desire.
All you have to do is :
   1. Install two wrappers for EntityFramework from Nuget.org

  •    CommunityEFProviderWrappers.EFProviderWrapperToolkit
  •    CommunityEFProviderWrappers.EFTracingProvider

   2. Create a DbConnection object that is configured to log SQL staements like the one below and inject this object into DbContext.

var context = new DbContext(CreateTracingConnection(connectionString, "System.Data.SqlClient");
        detail implementation of CreateTracingConnection is written below.


   3. EnableTracing for data context.

      ((IObjectContextAdapter)this).ObjectContext.EnableTracing();

By this approach, you definitely know what commands are being executed when your application is running in debug mode.




No comments:

Post a Comment