简体   繁体   English

EFCore在上一个操作完成之前,在此上下文中启动了第二个操作

[英]EFCore A second operation started on this context before a previous operation completed

I got this error, but im unable to determine what is the real cause, therefore im unable to fix it? 我得到了这个错误,但我无法确定究竟是什么原因,因此我无法修复它? "Entity Framework Core: A second operation started on this context before a previous operation completed" “实体框架核心:在上一个操作完成之前,在此上下文中启动了第二个操作”

The context 上下文

private readonly ApplicationDbContext _context;

public MyController(ApplicationDbContext context) 
{
    _context = context;
}

The error occurs here "await _context.SaveChangesAsync();", however this statement is executed only once. 此处出现错误“await _context.SaveChangesAsync();”,但此语句只执行一次。

        //Find user by Id
        var foundUser = await _context.Users.FindAsync(myUserId);

        //Populate myUserData here
         ....

        //If user not found, create the user
        if (foundUser == null)
        {
            _context.Users.Add(myUserData);
            await _context.SaveChangesAsync(); //<--------ERROR HERE!
        }

You are trying to modify foundUser , but it's not returned yer becuase it's async. 您正在尝试修改foundUser,但它不会返回,因为它是异步的。

The easier way to fix this is using .Result like this: 解决这个问题的更简单方法是使用.Result,如下所示:

if (foundUser.Result == null)

This way it will wait for the result of the previous call 这样它将等待前一次调用的结果

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (EFCore &amp; Identity) 在上一个操作完成之前,在此上下文中启动了第二个操作 - (EFCore & Identity) A second operation started on this context before a previous operation completed UserManager 错误“在上一个操作完成之前,在此上下文中启动了第二个操作” - UserManager error "A second operation started on this context before a previous operation completed" 在先前的异步操作完成之前,在该上下文上开始第二操作 - A second operation started on this context before a previous asynchronous operation completed Blazor:在前一个操作完成之前在此上下文上启动了第二个操作 - Blazor: A second operation started on this context before a previous operation completed 在前一个异步操作完成之前在此上下文上启动了第二个操作 - Second operation started on this context before a previous asynchronous operation completed 异步方法:在上一个操作完成之前,在此上下文上启动了第二个操作 - Async method: Second operation was started on this context before a previous operation completed UOW-在先前的异步操作完成之前,第二个操作在此上下文上开始 - UOW - A second operation started on this context before a previous asynchronous operation completed 在第二次保存到上下文时,继续获取“在上一个操作完成之前在此上下文中启动第二个操作”。 - Keep Getting “A second operation started on this context before a previous operation completed” when saving to the context a second time. 使用依赖注入时如何解决“在上一个操作完成之前在此上下文中启动了第二个操作......”? - How to fix 'A second operation started on this context before a previous operation completed…' when working with dependency injection? 如何解决错误 在上一个操作完成之前,在此上下文上启动了第二个操作 - How to solve the error A second operation started on this context before a previous operation completed
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM