这是我的代码:
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 中的多异步?
发布评论