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.

Phil

3 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 http://data.uservoice.com/forums/72025-ado-net-entity-framework-ef-feature-suggestions/suggestions/1015385-support-for-asynchronous-queries.

    Do you use this now on production?

  2. tugberk says:

    please also see this: http://stackoverflow.com/a/9434687/463785


    “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
    }.ToString();

Trackbacks/Pingbacks

  1. EF ObjectQuery<T> Context, Parameters, Connection properties equivalent on DbSet<T> - [...] Here is an instance: http://philsversion.com/2011/09/07/async-entity-framework-queries [...]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

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=""> <strike> <strong>