EF 5:如何取消longrunning查询的异步任务 [英] EF 5: How to cancel a longrunning query in a async Task
问题描述
在我的应用程序有多个选项卡,从与实体框架5。
In my application I have multiple tabs, displaying data from a database with Entity Framework 5.
当我在标签之间切换,我开始自动加载通过任务中的数据,因为我不想让图形用户界面变得无法响应(此任务需要大约5-10秒):
When I switch between the tabs I start auto loading the data via a Task, because I don't want the GUI to become unresponsive (this task takes about 5-10 seconds):
public async void LoadData()
{
[...]
await Task.Run(
() =>
{
Measurements = DataContext.Measurements
.Where(m => m.MeasureDate = DateTime.Today)
.ToList();
});
[...]
}
不过,虽然任务运行的用户仍然可以切换到另一个选项卡,如果他不说,我想取消EF查询和/或任务。
But while the task runs the user still can change to another tab, and if he does that I would like to cancel the EF query and/or the Task.
什么是实现这一目标的最佳方式是什么?
What would be the best way to accomplish this?
推荐答案
在EF5,有没有办法取消查询,因为它不接受的CancellationToken
。你可以阅读更多关于此这里:<一href="http://social.msdn.microsoft.com/Forums/en-US/ad3db060-190a-43a5-bff7-db6fc818a036/entity-framework-cancel-long-running-query"相对=nofollow>实体框架取消长时间运行的查询
In EF5, there is no way to cancel the queries since it doesn't accept CancellationToken
. You can read more about this here: entity framework cancel long running query
不过,EF6不支持它。它拥有的所有方法异步版本。因此,了ToList()
也可以是符合 ToListAsync()
对于长时间运行的查询,它确实有支持的CancellationToken
。
However, EF6 does support it. It has async version of all methods. So ToList()
could instead be ToListAsync()
for long running queries and it does have support for CancellationToken
.
PS :EF 6仍处于测试阶段,截至目前
P.S: EF 6 is still in beta as of now.
这篇关于EF 5:如何取消longrunning查询的异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!