[英]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.