Disabling Change Tracking in Entity Framework

February 20, 2009

Disabling Change Tracking in Entity Framework

Entity Framework came alongUsing NoTracking in Entity Framework
with a very helpful change
tracking
system. But with
great power comes great
responsibility. The responsibility
of the developers is to make
sure that the change tracking option will not be responsible to bad performance.

How to Disable the Change Tracking Option

In Entity Framework, change tracking is being done by the ObjectStateManager.
The ObjectStateManager maintains object state and identity management
for entity type instances and relationship instances. When you retrieve data
that is read only or in ASP.NET applications you should consider disabling the
change tracking option. Doing so will increase the performance of your system
in some situations. In order to disable change tracking you use the
MergeOption.NoTracking option of the MergeOption enumeration. Using that
option will retrieve entities in a detached state.
One place to disable the change tracking is when we retrieve data with the
ObjectQuery object. One of the overloaded constructors of the ObjectQuery
gets as a parameter the MergeOption enumeration. The following example
shows how to do it:

using (SchoolEntities context = new SchoolEntities())
{                
    var query = new ObjectQuery<Course>(
        "SELECT VALUE c FROM SchoolEntities.Course AS c", 
        context, 
        MergeOption.NoTracking);
 
    foreach (var course in query)
    {
        // the courses we enumerate are detached state
        Console.WriteLine("{0} {1}", course.CourseID, course.Title);
    }
}

Another way to disable the change tracking  is by setting the

MergeOption for the relevant EntitySet. The following example

shows how to do it:

using (SchoolEntities context = new SchoolEntities())
{
    context.Course.MergeOption = MergeOption.NoTracking;
 
    foreach (var course in context.Course)
    {
        // the courses we enumerate are detached state
        Console.WriteLine("{0} {1}", course.CourseID, course.Title);
    }
}

Summary

Lets sum up, sometimes it’s very helpful to disable the change tracking 
option of Entity Framework. These occasions are when we use read

only entities or in ASP.NET applications that don’t need the change

tracking
option. The disabling of the change tracking can help to increase

the performance of the system because the application will not use the

ObjectStateManager.

DotNetKicks Image
Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

one comment

  1. AlexanderMarch 27, 2009 ב 16:56

    Yes, but you can’t by my opinion disable change tracking when you call stored procedure in entity framework

    Reply