# Tracking vs. No-Tracking
# No-tracking queries
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 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 :
using (var context = new BookContext())
{
var book = context.Books.FirstOrDefault(b => b.BookId == 1);
book.Rating = 5;
context.SaveChanges();
}
# Tracking and projections
Example :
In the following query, which returns an `anonymous type`, the instances of `Book` in the result set `will be tracked`
using (var context = new BookContext())
{
var book = context.Books.Select(b => new { Book = b, Authors = b.Authors.Count() });
}
If the result set `does not` contain any `entity` types, then `no tracking` is performed
Example :
In the following query, which returns an `anonymous type` with some of the values from the entity (but `no instances` of the actual `entity` type), there is **no tracking** performed.
using (var context = new BookContext())
{
var book = context.Books.Select(b => new { Id = b.BookId, PublishedDate = b.Date });
}
# 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()
.