Entity Framework 6 中的多异步? [英] Multi-async in Entity Framework 6?

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

问题描述

这是我的代码:

var banner = context.Banners.ToListAsync()
var newsGroup = context.NewsGroups.ToListAsync()
await Task.WhenAll(banner, newsGroup);

但是当我从控制器调用函数时.显示错误

But when i called the function from controller. It showed error

在前一个异步操作完成之前,第二个操作在此上下文中开始.使用 'await' 确保在此上下文上调用另一个方法之前任何异步操作都已完成.不保证任何实例成员都是线程安全的.

A second operation started on this context before a previous asynchronous operation completed. Use 'await' to ensure that any asynchronous operations have completed before calling another method on this context. Any instance members are not guaranteed to be thread safe.

请帮我解决这个问题.

推荐答案

异常清楚地解释了每个上下文一次只允许一个异步操作.

The exception explains clearly that there is only one asynchronous operation per context allowed at a time.

因此,您必须一次await他们,如错误消息所示:

So, you either have to await them one at a time as the error message suggests:

var banner = await context.Banners.ToListAsync();
var newsGroup = await context.NewsGroups.ToListAsync();

或者您可以使用多个上下文:

Or you can use multiple contexts:

var banner = context1.Banners.ToListAsync();
var newsGroup = context2.NewsGroups.ToListAsync();
await Task.WhenAll(banner, newsGroup);

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

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