EntityFramework(6)和async(waitingForActivation)? [英] EntityFramework (6) and async ( waitingForActivation)?
问题描述
我下载了 EF6 (为了使用 async
)
所以我写了这个简单的方法:
public async任务< List< int>> MyasyncMethod()
{
var locations = await MyDumpEntities.AgeGroups.Select(f => f.endYear).ToListAsync();
返回地点;
}
...以后...
DumpEntities1 MyDumpEntities = new DumpEntities1();
var data = MyDumpEntities.AgeGroups.ToListAsync();
MyasyncMethod()。ContinueWith(s => {Response.Write(f);});
MyDumpEntities.Dispose();
但我没有看到屏幕上的任何东西,当我检查数据
我看到这个:
ps这是 ToListAsync
签名
我缺少什么?
根据您的问题的注释和行:
var data = MyDumpEntities.AgeGroups.ToListAsync() ;
什么将数据
类型是? 任务<列表与LT; AgeGroup>>
。没错,列表< AgeGroup>
。
所以,您必须将 Page_Load
标记为异步(如果可能):
public async void Page_Load(object sender,EventArgs e)
{
using(var MyDumpEntities = new DumpEntities1())
{
var data = await MyDumpEntities.AgeGroups.ToListAsync();
}
}
或者等待执行某种方式(继续,阻止等待)另一件事(别人可能想纠正,如果我错了),但是由于你使用第二次调用的延续,所以我会很仔细处理在连续之外的上下文。这可能会导致您先前处理上下文。在这种特殊情况下,您不会在延续中使用上下文,但看起来很可疑...
所以我要么是
MyasyncMethod()。ContinueWith(s => {Response.Write(f); MyDumpEntities.Dispose();});
或者只是使用 async
p>
var result = await MyasyncMethod();
Response.Write(f);
MyDumpEntities.Dispose();
并添加 Async =True
页面指令
I've downloaded EF6 ( in order to use async
)
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();
But I don't see anything on the screen and when I inspect data
I see this :
p.s. this is the ToListAsync
signature
What am I missing ?
Basing it of off the comments and the line that you have problem with:
var data = MyDumpEntities.AgeGroups.ToListAsync();
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).
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...
So I'd either
MyasyncMethod().ContinueWith(s => { Response.Write("f"); MyDumpEntities.Dispose();});
Or just use async
there too
var result = await MyasyncMethod();
Response.Write("f");
MyDumpEntities.Dispose();
and add Async="True"
to the page directive
这篇关于EntityFramework(6)和async(waitingForActivation)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!