【发布时间】:2019-06-21 16:09:18
【问题描述】:
我收到了这个错误,但我无法确定真正的原因是什么,因此我无法修复它? “Entity Framework Core:在前一个操作完成之前在此上下文上启动了第二个操作”
上下文
private readonly ApplicationDbContext _context;
public MyController(ApplicationDbContext context)
{
_context = context;
}
这里出现错误“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!
}
【问题讨论】:
-
可能不相关,但在这种模式下,
_context是如何处理的? -
上面的代码在一个方法中,我在没有“await”的情况下使用它,我添加了“await”,现在一切正常。
标签: c# asp.net-core entity-framework-core