Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Entity Framework support parallel async queries? [duplicate]

What happens when we start multiple async Entity Framework queries and run them in parallel?

Are they physically executed in parallel? Are they serialized by Entity Framework? Is this unsupported? Does it result in an exception?

public async Task QueryDatabase() {     using (var context = new MyDbContext())     {         Task task1 = context.SomeTable1.ToListAsync();         Task task2 = context.SomeTable2.ToListAsync();          await Task.WhenAll(task1, task2);     } } 
like image 443
usr Avatar asked Jul 11 '14 16:07

usr


People also ask

When to use async EF Core?

In general, one would use async await when you want to keep the currently running thread from blocking. This frees the thread for other tasks (like updating the UI), while we await the asynchronous result.

What is the advantage of Entity Framework over ADO Net?

In ADO.Net, it performs bulk data SQL operations and reporting. On the other hand, the entity framework performs the CRUD operations in a project. 9. It is more flexible in terms of raw SQL queries and procedures because it has more control on the database.

Is LINQ async?

Note that there are no async versions of some LINQ operators such as Where or OrderBy, because these only build up the LINQ expression tree and don't cause the query to be executed in the database. Only operators which cause query execution have async counterparts.

Does Entity Framework Core support multiple operations on the same context?

Entity Framework Core does not support this scenario either. 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.

Can Entity Framework execute multiple queries at the same time?

Entity Framework Execute multiple queries async and in parallel. Example. When using async queries, you can execute multiple queries at the same time, but not on the same context. If the execution time of one query is 10s, the time for the bad example will be 20s, while the time for the good example will be 10s.

Where are the async methods in Entity Framework Core defined?

The EF Core async extension methods are defined in the Microsoft.EntityFrameworkCore namespace. This namespace must be imported for the methods to be available. The async LINQ operators discussed above can only be used on EF queries - you cannot use them with client-side LINQ to Objects query.

What is async and await in EF Core?

Following the .NET standard, EF Core provides asynchronous counterparts to all synchronous methods which perform I/O. These have the same effects as the sync methods, and can be used with the C# async and await keywords.


Video Answer


1 Answers

This is not supported as per the specifications of version 6.

This should throw a DbConcurrencyException exception saying

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 a codeplex page of the project:

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.

Entity Framework Core does not support this scenario either.

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.

like image 145
ken2k Avatar answered Sep 21 '22 10:09

ken2k