[英]EF Core async client side evaluation
Working with net5.0
.使用
net5.0
。 So I have this query:所以我有这个查询:
context.DbSet
.Where() // server side
.AsEnumerable()
.Where() // can't be translated to SQL so client side
.ToList();
So how can we achieve an async variant of this?那么我们如何实现这个的异步变体呢? I would assume it would work like this:
我会假设它会像这样工作:
await context.DbSet
.Where() // server side
.AsAsyncEnumerable()
.Where() // can't be translated to SQL so client side
.ToListAsync();
But that doesn't work.但这不起作用。 What is the right approach?
什么是正确的方法?
As mentioned in the Explicit client evaluation section of the EF Core documentation, the EF Core team vision is that如 EF Core 文档的显式客户端评估部分所述,EF Core 团队的愿景是
If you are using
AsAsyncEnumerable
and want to compose the query further on client side then you can use System.Interactive.Async library which defines operators for async enumerables.如果您正在使用
AsAsyncEnumerable
并希望在客户端进一步编写查询,那么您可以使用System.Interactive.Async库,它定义了异步枚举的运算符。 For more information, see client side linq operators .有关详细信息,请参阅客户端 linq 运算符。
So by idea you need to install that package and use因此,您需要安装 package 并使用
await context.DbSet
.Where() // server side
.AsAsyncEnumerable()
.Where(...) // this is provided by System.Interactive.Async
.ToList(); // as well this
Just please note that there is some method naming clash when you reference both packages ( System.Interactive.Async
and Microsoft.EntityFrameworkCore
, because EF Core DbSet<T>
implements both IEnumerable<T>
and IAsyncEnumerable<T>
, which makes some extension method calls ambiguous), so you might start getting compile time errors.请注意,当您引用两个包(
System.Interactive.Async
和Microsoft.EntityFrameworkCore
时,存在一些方法命名冲突,因为 EF Core DbSet<T>
实现了IEnumerable<T>
和IAsyncEnumerable<T>
,这使得一些扩展方法调用模棱两可),因此您可能会开始遇到编译时错误。 There are open issues in their GitHub issue trackers, but neither team is willing to fix them saying that the problem is caused by the other.他们的 GitHub 问题跟踪器中存在未解决的问题,但两个团队都不愿意修复他们说问题是由另一个问题引起的。 One of the drawbacks of the open source policy utilized recently by Microsoft.
微软最近使用的开源政策的缺点之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.