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

Does Entity Framework support parallel async queries?(Entity Framework 是否支持并行异步查询?)
本文介绍了Entity Framework 是否支持并行异步查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们启动多个异步实体框架查询并并行运行它们时会发生什么?

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

推荐答案

根据 第 6 版规范.

这应该抛出一个 DbConcurrencyException 异常说

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 将检测开发者是否尝试一次执行两个异步操作并抛出.

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

来自项目的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.

Entity Framework Core 也不支持这种情况.

Entity Framework Core does not support this scenario either.

EF Core 不支持在同一个上下文实例上运行多个并行操作.在开始下一个操作之前,您应该始终等待操作完成.这通常通过在每个异步操作上使用 await 关键字来完成.

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.

这篇关于Entity Framework 是否支持并行异步查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)