Async Entity Framework Queries

I’ve been writing a lot of Node.js recently, and I’ve really love how the asynchronous database access works, and thought to myself “I wonder if I can do that with Entity Framework?”

While I couldn’t find anywhere in EF the inherently support any form of async calls, I did find that you can with the underlying SqlConnection (assuming you’re using MSSQL.)

So with a little bit of Async CTP magic mixed in, I created an extension method to read the data asynchronously.

Unfortunately I couldn’t find an obvious way to open the connection or write data asynchronously, but I think this is good step.


8 Responses to “ “Async Entity Framework Queries”

  1. tugberk says:

    Very nice implementation. I just voted for Support for asynchronous queries on ADO.NET Entity Framework (EF) Feature Suggestions site

    Do you use this now on production?

  2. tugberk says:

    please also see this:

    “SQL Command is not truly asynchronous until you enable Asynchronous Processing=true on the connection string. While this is not set (and by default is not) your ‘asyncronous’ calls to BeginExecuteReader are nothing but a sham, the call will launch a thread and block that thread. When true async processing is enabled in the connection string then the call is truly async and the callback is based on IO completion.”

  3. tugberk says:

    Ahh, sorry! You already added that :s

    cmd.Connection.ConnectionString = new SqlConnectionStringBuilder(cmd.Connection.ConnectionString)
    AsynchronousProcessing = true

  4. You can use OpenAsync in .NET 4.5 (which you’re using since you’re using async/await) to open the connection asynchronously –

  5. G. Stoynev says:

    This is a handy implementation.

    How do you cleanup at the end, when reader is done?

  6. ść.


    Nie pamiętała, podczas gdy wydostała się Olśniewająca maszyny.
    zebudowa (
    śmierdzącej kałuży, co przyszykowało, że

    siedziała opartga o niezdolny do odczuwania ściana kamieniczki.
    Zupa, co zawadziła, owo pochylająca

    siępowyżej nią facjata z wymykającym się spod misiurki
    kosmykiem jasnych włosów.

    Opuściła makówkę, tłuste strąki włosów zasłoniły

    Wiązka samowolny spod misiurki nie był przejrzysty,
    był białoszary. Owe nie.

  7. When I initially left a comment I appear to have
    clicked the -Notify me when new comments are added- checkbox and from now on
    each time a comment is added I receive 4 emails with the
    exact same comment. Perhaps there is an easy method you can
    remove me from that service? Many thanks!

  8. Hey! Someone in my Myspace group shared this website with us so I came to check it out.
    I’m definitely loving the information. I’m book-marking and
    will be tweeting this to my followers! Terrific blog and great style and design.


  1. EF ObjectQuery<T> Context, Parameters, Connection properties equivalent on DbSet<T> - [...] Here is an instance: [...]