DCSIMG
Disabling Change Tracking in Entity Framework - Gil Fink's Blog

Gil Fink's Blog

Fink about IT

News

Microsoft MVP

My Facebook Profile My Twitter Profile My Linkedin Profile

Locations of visitors to this page

Creative Commons License

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
© Copyright 2013 Gil Fink

Hebrew Articles

Index Pages

My OSS Projects

English Articles

Disabling Change Tracking in Entity Framework

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

Comments

DotNetKicks.com said:

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# February 20, 2009 3:58 PM

Disabling Change Tracking in Entity Framework - DotNetBurner said:

DotNetBurner.com - news and articles about .net DotNetBurner

# February 20, 2009 4:05 PM

Performance Tracker Geo Tracker Converible Conversion Kit Geo Tracker Proportioning Valve | Tiresdirect said:

Pingback from  Performance Tracker Geo Tracker Converible Conversion Kit Geo Tracker Proportioning Valve | Tiresdirect

# February 20, 2009 5:32 PM

Alexander said:

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

# March 27, 2009 4:56 PM

Disabling Change Tracking in Entity Framework said:

Pingback from  Disabling Change Tracking in Entity Framework

# February 25, 2013 8:56 AM