Using DbContext in EF 4.1 Part 11: Load and AsNoTracking

 


The information in this post is out of date.

Visit msdn.com/data/ef for the latest information on current and past releases of EF.

For Load see https://msdn.com/data/jj592911

For AsNoTracking see https://msdn.com/data/jj556203


 

Introduction

Version 4.1 of the Entity Framework contains both the Code First approach and the new DbContext API. This API provides a more productive surface for working with the Entity Framework and can be used with the Code First, Database First, and Model First approaches. This is the eleventh post of a twelve part series containing collections of patterns and code fragments showing how features of the new API can be used.

The posts in this series do not contain complete walkthroughs. If you haven’t used EF 4.1 before then you should read Part 1 of this series and also Code First Walkthrough or Model and Database First with DbContext before tackling this post.

Load

In several of the parts in this series we have wanted to load entities from the database into the context without immediately doing anything with those entities. A good example of this is loading entities for data binding as described in Part 7. One common way to do this is to write a LINQ query and then call ToList on it, only to immediately discard the created list. The Load extension method works just like ToList except that it avoids the creation of the list altogether.

Here are two examples of using Load. The first is taken from a Windows Forms data binding application where Load is used to query for entities before binding to the local collection, as described in Part 7:

 protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    
    _context = new ProductContext();

    _context.Categories.Load();
    categoryBindingSource.DataSource =
        _context.Categories.Local.ToBindingList();
}

The second example shows using Load to load a filtered collection of related entities, as described in Part 6:

 using (var context = new UnicornsContext())
{
    var princess = context.Princesses.Find(1);

    // Load the unicorns starting with B related to a given princess
    context.Entry(princess)
        .Collection(p => p.Unicorns)
        .Query()
        .Where(u => u.Name.StartsWith("B"))
        .Load();
}

No-tracking queries

Sometimes you may want to get entities back from a query but not have those entities be tracked by the context. This may result in better performance when querying for large numbers of entities in read-only scenarios. A new extension method AsNoTracking allows any query to be run in this way. For example:

 using (var context = new UnicornsContext())
{
    // Query for all unicorns without tracking them
    var unicorns1 = context.Unicorns.AsNoTracking();

    // Query for some unitcorns without tracking them
    var unicorns2 = context.Unicorns
                        .Where(u => u.Name.EndsWith("ky"))
                        .AsNoTracking()
                        .ToList();
} 

In a sense Load and AsNoTracking are opposites. Load executes a query and tracks the results in the context without returning them. AsNoTracking executes a query and returns the results without tracking them in the context.

Summary

In this part of the series we looked the Load extension method for loading entities into the context and the AsNoTracking extension method for returning entities without tracking them.

As always we would love to hear any feedback you have by commenting on this blog post.

For support please use the Entity Framework Forum.

Arthur Vickers

Developer

ADO.NET Entity Framework