多异步实体框架6? [英] Multi-async in Entity Framework 6?
问题描述
这是我的code:
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
开始在此背景下的第二个操作的previous异步操作完成之前。使用等待,以确保调用此背景下的另一种方法之前,任何异步操作已经完成。任何实例成员不能保证是线程安全的。
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.
所以,你要么等待作为错误信息提示
逐一时间:
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);
这篇关于多异步实体框架6?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!