Tracking vs. No-Tracking
Remarks#
Tracking behavior controls whether or not Entity Framework will keep information about an entity instance in its change tracker. If an entity is tracked, any changes detected in the entity will be persisted to the database during SaveChanges().
Tracking queries
- By default, queries that return entity types are tracking
- This means you can make changes to those entity instances and have
those changes persisted by
SaveChanges()
Example :
-
The change to the
bookrating will be detected and persisted to the database duringSaveChanges().using (var context = new BookContext()) { var book = context.Books.FirstOrDefault(b => b.BookId == 1); book.Rating = 5; context.SaveChanges(); }
No-tracking queries
- No tracking queries are useful when the results are used in a
read-onlyscenario - They are
quicker to executebecause there is no need to setup change tracking information
Example :
using (var context = new BookContext())
{
var books = context.Books.AsNoTracking().ToList();
}With EF Core 1.0 you are also able to change the default tracking behavior at the context
instance level.
Example :
using (var context = new BookContext())
{
context.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.NoTracking;
var books = context.Books.ToList();
}Tracking and projections
- Even if the result type of the query isn’t an entity type, if the
result
contains entitytypes they will still betracked by default
Example :
-
In the following query, which returns an
anonymous type, the instances ofBookin the result setwill be trackedusing (var context = new BookContext()) { var book = context.Books.Select(b => new { Book = b, Authors = b.Authors.Count() }); } -
If the result set
does notcontain anyentitytypes, thenno trackingis performed
Example :
-
In the following query, which returns an
anonymous typewith some of the values from the entity (butno instancesof the actualentitytype), there is no tracking performed.using (var context = new BookContext()) { var book = context.Books.Select(b => new { Id = b.BookId, PublishedDate = b.Date }); }