Entity Framework是否支持并行异步查询?

时间:2023-01-18 16:29:49

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);
    }
}

2 个解决方案

#1


52  

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

根据版本6的规范,不支持此功能。

This should throw a DbConcurrencyException exception saying

这应该抛出一个DbConcurrencyException异常说

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.

在先前的异步操作完成之前,在该上下文上开始第二个操作。使用'await'确保在此上下文上调用另一个方法之前已完成任何异步操作。任何实例成员都不保证是线程安全的。

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

EF将检测开发人员是否尝试同时执行两个异步操作并抛出。

From a codeplex page of the project:

从项目的codeplex页面:

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.

启用数据库操作的异步执行实际上与在同一上下文中启用并发执行是正交的。在服务器方案的特定情况下,使用并发访问会对可伸缩性产生负面影响,因为这意味着为了处理单个请求,您将旋转任意数量的不同线程。所有线程都会争夺内存等资源与服务其他并发请求所需的其他线程。

#2


2  

Just a note, as mentioned by ken2k this is not allowed when using Entity Framework with MS SQL Server. However, if you are using Entity Framework with Oracle, this is allowed.

只是一个注释,正如ken2k所提到的,当使用实体框架和MS SQL Server时,这是不允许的。但是,如果您将实体框架与Oracle一起使用,则允许这样做。

#1


52  

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

根据版本6的规范,不支持此功能。

This should throw a DbConcurrencyException exception saying

这应该抛出一个DbConcurrencyException异常说

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.

在先前的异步操作完成之前,在该上下文上开始第二个操作。使用'await'确保在此上下文上调用另一个方法之前已完成任何异步操作。任何实例成员都不保证是线程安全的。

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

EF将检测开发人员是否尝试同时执行两个异步操作并抛出。

From a codeplex page of the project:

从项目的codeplex页面:

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.

启用数据库操作的异步执行实际上与在同一上下文中启用并发执行是正交的。在服务器方案的特定情况下,使用并发访问会对可伸缩性产生负面影响,因为这意味着为了处理单个请求,您将旋转任意数量的不同线程。所有线程都会争夺内存等资源与服务其他并发请求所需的其他线程。

#2


2  

Just a note, as mentioned by ken2k this is not allowed when using Entity Framework with MS SQL Server. However, if you are using Entity Framework with Oracle, this is allowed.

只是一个注释,正如ken2k所提到的,当使用实体框架和MS SQL Server时,这是不允许的。但是,如果您将实体框架与Oracle一起使用,则允许这样做。