Async inside DataContext

Here we go, yesterday’s discussion about the correct usage a workflow to get data.


using(DbContext ctx = ...) {

    return ctx.RnDTable.ToListAsync();


… really “unschön”

and what’s the problem with the above code? looks fine at (my) first sight

Oct 18

the context might gets disposed, while the query is still running, resulting in a ContextDisposedException

 Oct 18

Never – in my opinion – it can not be disposed while the query is running. Can you prove what you say? I’m determined to see this thing in details.

Oct 18

As you can see, it does happen.

Oct 18

🤔Not fully convinced yet, will try myself soon. Also task.Result is ugly, you’re supposed to show an await, that’s the topic. Give me a couple of hours max to elaborate your code. Thank you very much for your reply in the meantime 

Oct 18

Next try to convince you 😉


Oct 18

Perfect, I’ve not yet opened VS 😉 A couple of doubts 1) I rarely use async but very often I return that way synchronously and I’ve never seen a context disposed in the sync equivalent: have you? 2) could one blame the specific datacontext implementation (is it EF or what)?

Oct 18

You’re right but notice that the sync version does work as expected 👆

Oct 18

and now the *async* version *does* work => you’re no longer right! 😉

Oct 18

So – in conclusion, from waht I can see – the problem here is that one has to return await ... ToListAsync();

inside the db data connection (await all the way down, starting from the inner method calling ToListAsync) and there is no issue with any using/dispose statement.


3 thoughts on “Async inside DataContext

  1. Allow me to give some input on the discussion: the pattern for the Dispose method is such that requires Dispose to be explicitly called. Besides this, Dispose is meant to handle the release of unmanaged resources, so, there are no other responsibilities at hand pertaining to Dispose, including the fact that the reference to the instance remains active, so, while a long running operation is running in async or parallel mode for a given reference, any other method could call the Dispose method of the said reference … that would be a buggy method in my book, but it is possible.

    Kind regards, GEN


    • Here we go. What you mean by “any other method could call the Dispose methof of said reference”? The reference is the DataContext and it is defined inside a “using” statement. So only the closure of such statement is disposing the reference. No, the final code posted is not buggy, it does really work as expected. The using statement has the goal to avoid an explicit call to Dispose, that’s the whole point of it (look for example at but there are tons of docs about this)


  2. New tweet about this topic here

    and follow the link to the related github discussion
    my repo with SqlClient counter-example:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s