gpt4 book ai didi

c# - EntityFramework ToListAsync() 不起作用

转载 作者:太空狗 更新时间:2023-10-29 19:55:53 25 4
gpt4 key购买 nike

我尝试调用 EF 方法 ToListAsync。但什么也没发生——没有异常(exception),只是运行没有超时。

这是我的代码。

        private IQueryable<Place> placeCompleteQuery;
protected IQueryable<Place> PlaceCompleteQuery
{
get
{
return this.placeCompleteQuery ?? (this.placeCompleteQuery = this.Context.Places.Include(p => p.Address).
Include(p => p.CreatedBy).
Include(p => p.Source).
Include(p => p.Type.Translations).
Include(p => p.Ratings));
}
}

public async Task<IList<Place>> GetPlacesByLocationAsync(DbGeography location, int radius)
{
List<Place> temporaryResult = PlaceCompleteQuery.Where(p => p.Location.Distance(location) <= radius).
ToList();

return await PlaceCompleteQuery.Where(p => p.Location.Distance(location) <= radius).
ToListAsync();
}

ToList 方法的第一次同步调用立即返回结果。 ToListAsync 的第二次异步调用仍在运行,没有结果也没有异常。

有什么建议吗?

最佳答案

我怀疑在你的调用栈中,你的代码正在调用 Task.WaitTask<T>.Result .如果您在 UI 线程上或从 ASP.NET 请求上下文执行此操作,your code will deadlock ,正如我在我的博客上解释的那样。

要修复它,请使用 await而不是 Task.WaitTask<T>.Result .

关于c# - EntityFramework ToListAsync() 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22016484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com