的EntityFramework(6)和异步(waitingForActivation)? [英] EntityFramework (6) and async ( waitingForActivation)?
问题描述
我已经下载 EF6 (以为了使用异步
)
所以我写了这个简单的方法:
So I wrote this simple method :
public async Task<List<int>> MyasyncMethod()
{
var locations = await MyDumpEntities.AgeGroups.Select(f=>f.endYear).ToListAsync();
return locations;
}
...Later...
DumpEntities1 MyDumpEntities = new DumpEntities1();
var data = MyDumpEntities.AgeGroups.ToListAsync();
MyasyncMethod().ContinueWith(s => { Response.Write("f"); });
MyDumpEntities.Dispose();
不过,我没有看到任何屏幕上,当我检查数据
我看到这一点:
的 P.S。这是 ToListAsync
签名的
p.s. this is the ToListAsync
signature
我是什么失踪?
推荐答案
此基础它关闭,您有问题的意见和行:
Basing it of off the comments and the line that you have problem with:
var data = MyDumpEntities.AgeGroups.ToListAsync();
会有什么数据
类型? 任务&LT;名单,LT; AgeGroup&GT;&GT;
。这是正确的,而不是列表与LT; AgeGroup&GT;
。
所以,你要么必须标记的Page_Load
为异步(如果可能):
What will data
type be? Task<List<AgeGroup>>
. That's right, not List<AgeGroup>
.
So, you either have to mark the Page_Load
as async (if possible):
public async void Page_Load(object sender, EventArgs e)
{
using(var MyDumpEntities = new DumpEntities1())
{
var data = await MyDumpEntities.AgeGroups.ToListAsync();
}
}
或等待执行以某种方式(延续,阻塞等)。
Or wait for the execution somehow (continuation, blocking wait).
另一件事(别人可能要纠正,如果我错了),但由于您使用延续了第二个电话,我会非常小心处理延续之外的上下文。它可能会变成你处置方面preemptively。在这种特殊情况下,你不使用延续的背景下,但看起来可疑...
Another thing (someone else might want to correct that if I'm wrong), but since you're using continuation for the second call, I would be very careful of disposing the context outside of continuation. It might turn out that you dispose the context preemptively. In this particular case, you're not using the context in the continuation, but it looks suspicious...
所以我想无论是
MyasyncMethod().ContinueWith(s => { Response.Write("f"); MyDumpEntities.Dispose();});
或者只是使用异步
有太多
var result = await MyasyncMethod();
Response.Write("f");
MyDumpEntities.Dispose();
和增加异步=真
页面指令
这篇关于的EntityFramework(6)和异步(waitingForActivation)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!