3

I have problem with asynchronous controller in ASP.Net MVC 5 application. I'm using Entity Framework 6 Code First approach.

I have a method

public async Task<ActionResult> Index()
{
    using(var context = new MyDbContext())
    {
        var eventsTask = context.Events
            .Where(e => e.Enable)
            .ToListAsync();

        var countTask = context.Users
            .CountAsync();

        await Task.WhenAll(eventsTask, countTask);
        return View(new ViewModel()
        {
            Events = eventsTask.Result, 
            Count = countTask.Result
        });
    }
}

I have two asyncronous methods here. I have measured each of them separately via MiniProfiler. They takes ~85 ms.

But in my method I run them using Task.WhenAll(). I believe it executes Db queries asynchronously and should take about ~85-90 ms for both. But it takes ~170-180. So I have got asynchronous methods run synchronously (following each other).

I think it is because of context. I have a test, when I remove context queries and call many api methods using HttpClient. It takes time equals to longer of them (3 api calling, ~500 ms each of them. Totally method takes ~600 ms). I believe that It is possible to execute EF methods asynchronously.

Does anyone know the solution

4
  • Does EF even support parallel operations? Probably it just serializes them. Commented Jul 11, 2014 at 16:08
  • Try using different contexts for each task and post the timing. Commented Jul 11, 2014 at 16:16
  • possible duplicate of Mutil async entity framework 6? Commented Jul 11, 2014 at 16:20
  • I have read this post. I tried different context, but have got the same result. Commented Jul 11, 2014 at 17:00

2 Answers 2

7

This shouldn't even work actually, but throw an exception instead. I'm guessing the first query completes before the second one even starts.

EF6 doesn't support multiple async operations on the same context.

Either await each query (so they won't run concurrently), or use a different context for each query.

Sign up to request clarification or add additional context in comments.

2 Comments

It works fine, without any . But also I have tried to use different context I had the same result.
@GFoley83 Not sure why you linked this, but the described answer won't work
0

I found the problem.

The reason is a MiniProfiler.EF6. 1) To measure my sql question I use MiniProfiler.EF6. This framework avoid concurrent sql queries even I use different EF DBContext.

2) I disable MiniProfile.EF6 and run my application. I have got an exception about that ken2k mentioned. To avoid this exception I follow this answer EF6 doesn't support multiple async operations on the same context.

3) I measure Dapper and concurrent SQL queries. I have used a single SQLConnection for asyncronous queries in my tests. I have had the results:

a) if i use SqlConnection for Dapper then queries execute
asynchronous (parallel)

b) if i use ProfiledDbConnection from
MiniProfiler then Dapper execute queries follow each others (not
parallel)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.