EF 6: Async

2013/03/19

EF 6: Async

this post is the first in a series about what’s new in EF 6.

great improvements are about to come with Entity Framework 6.
it is a major release and the first one since EF become an open source.
each post in the series will be dedicate to a single feature.

this post will focus on a new EF a-sync features.

EF, Parallel, async, await, task, api, entity

the first question that should be asked is, why do we need parallel data access?
moreover why do we need a dedicate parallel data access API, rather then using the TPL Task.Run(…) in order to introduce parallelism?

the answer is related both to thread safety and ThreadPool optimization.

a dedicate data access API for parallel programming, ensured a thread safe access, which may not be a trivial task.

furthermore, when the database is executing a query the .NET thread is idle (waiting for the execution result).
if this idle thread has taken from ThreadPool (either directly or through high level API like TPL Task.Run(…)) it could lead to a ThreadPool starvation.

assuming that you have a 2 second query, Task.Run(…) will hold the ThreadPool’s thread for 2 second even though it idle while the database is executing the query.

what you really want is an API that operate over IOCP (IO completion port). IOCP API will release the ThreadPool’s thread back to the pool and re-claim a thread at the completion time.

at the API level you can find a-sync operations everywhere.
I will present some of the main API.

the following snippets, will use the following entities (code first):

Code Snippet
  1. public class Author
  2. {
  3.     public Author()
  4.     {
  5.         Blogs = new List<Blog>();
  6.     }
  7.     public int Id { get; set; }
  8.     public string Name { get; set; }
  9.     public IList<Blog> Blogs { get; set; }
  10. }
  11.  
  12. public class Blog
  13. {
  14.     public int Id { get; set; }
  15.     public string Title { get; set; }
  16.     public string Content { get; set; }
  17.     public int AuthorId { get; set; }
  18.  
  19. }

and the following context:

Code Snippet
  1. public class Context : DbContext
  2. {
  3.     public DbSet<Author> Authors { get; set; }
  4.     public DbSet<Blog> Blogs { get; set; }
  5. }

a-sync save
Code Snippet
  1. using (var context = new Context())
  2. {
  3.     context.Authors.Add(author);
  4.     int affected = await context.SaveChangesAsync();
  5. }

notice the await at line 4.

a-sync read
Code Snippet
  1. using (var context = new Context())
  2. {
  3.     Author[] authors = await context.Authors
  4.         .Include(a => a.Blogs)
  5.         .ToArrayAsync();
  6.     return authors;
  7. }

line 5, is where the magic happens.

execute command
Code Snippet
  1. using (var context = new Context())
  2. {
  3.     int affected = await
  4.         context.Database.ExecuteSqlCommandAsync("DELETE FROM BLOGS");
  5. }

summary

EF 6 is having a great parallel API, which respect IOCP and compatible with the new async / await syntax.

 

EF, Parallel, async, await, task, api, entity,sdp

Shout it

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

2 comments

  1. boydcom.com2013/12/23 ב 21:20

    I truly love your website.. Pleasant colors & theme. Did you build
    this site yourself? Please reply back as I’m wanting to
    create my own personal website and would like to
    find out where you got this from or just what the theme is called.

    Appreciate it!

    Reply
  2. dor cohen2016/11/20 ב 13:04

    I’ve read a few good stuff here. Certainly value bookmarking for revisiting. I wonder how much attempt you put to create this sort of great informative web site.

    Reply