# Wednesday, 19 December 2012

At BUILD, during this session by Entity Framework Program Manager Rowan Miller, Entity Framework 6 beta was announced and made publicly available.  You can get the Entity Framework 6 beta bits via NuGet.  Currently they are only available via the Package Manager Console, more info can be found on the EF NuGet page

Entity Framework 6 is the first new version of EF since the announcement earlier this year that the product would be Open Sourced.  This means that not only is the source available but the builds of EF6 beta are available as soon as the code is checked in.  Entity Framework is hosted on CodePlex.

The EF 6 roadmap defines in detail what is currently in the works and what is still on the board as far as features and improvements for EF 6.  In this post I want to focus on one new addition to Entity Framework that I find very compelling.

One of the great features introduced in .NET 4.5 was the task-based asynchronous functionality using the async and await keywords.  Now with EF 6 comes support for asynchronous querying and saving using the same async and await coding convention.  Let’s first look at asynchronous querying.

The following asynchronous extension methods have been implemented for querying your data context and as you’d expect they are the asynchronous versions of their synchronous namesakes:

  • AllAsync
  • AnyAsync
  • AverageAsync
  • ContainsAsync
  • CountAsync
  • FindAsync
  • FirstAsync
  • FirstOrDefaultAsync
  • LoadAsync
  • LongCountAsync
  • MaxAsync
  • MinAsync
  • SingleAsync
  • SingleOrDefaultAsync
  • SumAsync
  • ToArrayAsync
  • ToDictionaryAsync
  • ToListAsync

In the code snippet below you can see an example of using the ForEachAsync method to asynchronously loop through all the manufacturers in my data context and print them to the console.

private static async Task PrintAllManufacturers() {
    using (DataContext context = new DataContext()) {
        await context.Manufacturers.ForEachAsync(m => Console.WriteLine("{0} : {1}", m.Name, m.Country));
    }
}

Another piece of Entity Framework functionality that the asynchronous goodness has been added too is saving. Below is an example of adding a manufacturer and asynchronously saving the changes.

private static async Task AddManufacturer(string name, string country) {
    using (DataContext context = new DataContext()) {
        context.Manufacturers.Add(new Manufacturer {Name = name, Country = country});
        await context.SaveChangesAsync();
    }
}

Using the asynchronous pattern is a great way to provide a more responsive user experience and now loading data asynchronously with Entity Framework has become much easier.

Name
E-mail
(will show your gravatar icon)
Home page

Comment (Some html is allowed: a@href@title, strike) where the @ means "attribute." For example, you can use <a href="" title=""> or <blockquote cite="Scott">.  

Enter the code shown (prevents robots):

Live Comment Preview