Entity Framework 6 中的多异步?

编程入门 行业动态 更新时间:2024-10-27 04:28:36
本文介绍了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 中的多异步?

本文发布于:2023-11-25 01:02:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1627678.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Entity   Framework

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!