EntityFramework(6)和异步(waitingForActivation)? [英] EntityFramework (6) and async ( waitingForActivation)?

查看:36
本文介绍了EntityFramework(6)和异步(waitingForActivation)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经下载了 EF6(为了使用 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();

但是我在屏幕上没有看到任何东西,当我检查 data 时,我看到了这个:

But I don't see anything on the screen and when I inspect data I see this :

ps.这是 ToListAsync 签名

我错过了什么?

推荐答案

基于评论和您遇到问题的行:

Basing it of off the comments and the line that you have problem with:

var data = MyDumpEntities.AgeGroups.ToListAsync();

data 类型是什么?任务<列表<年龄组>>.没错,不是List.因此,您必须将 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).

另一件事(如果我错了,其他人可能想更正),但是由于您在第二次调用中使用了延续,因此我会非常小心地处理延续之外的上下文.结果可能是您预先处理了上下文.在这种特殊情况下,您没有在延续中使用上下文,但它看起来很可疑...

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();});

或者在那里也使用 async

var result = await MyasyncMethod();
Response.Write("f");
MyDumpEntities.Dispose();

并将 Async="True" 添加到页面指令

and add Async="True" to the page directive

这篇关于EntityFramework(6)和异步(waitingForActivation)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆