Does Entity Framework support parallel async queries?

.net entity-framework entity-framework-6

Question

What occurs when several async Entity Framework queries are launched simultaneously?

Are they physically carried out simultaneously? Do they use Entity Framework serialization? Does this lack support? Is there an exception as a result?

public async Task QueryDatabase()
{
    using (var context = new MyDbContext())
    {
        Task task1 = context.SomeTable1.ToListAsync();
        Task task2 = context.SomeTable2.ToListAsync();

        await Task.WhenAll(task1, task2);
    }
}
1
53
7/11/2014 4:15:11 PM

Accepted Answer

According to the characteristics of version 6, this is not supported.

This ought to trigger aDbConcurrencyException the exception being

A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.

EF will detect if the developer attempts to execute two async operations at one time and throw.

From 22 to 22:

Enabling asynchronous execution of database operations is actually orthogonal to enabling concurrent execution on the same context. In the particular case of server scenarios, using concurrent access could affect scalability negatively as it would mean that in order to process a single request you would be spinning of an arbitrary number of different threads. All the threads would compete for resources such as memory with other threads necessary to server other concurrent requests.

neither supports this occurrence. Entity Framework Core

EF Core doesn't support multiple parallel operations being run on the same context instance. You should always wait for an operation to complete before beginning the next operation. This is typically done by using the await keyword on each async operation.

64
2/10/2020 6:16:03 AM

Popular Answer

Just a reminder that, as ken2k pointed out, utilizing Entity Framework with MS SQL Server does not permit this. This is permitted, though, if you're working with Oracle and Entity Framework.



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow