如何解决过滤器表达式中的 Cannot await?

How to resolve the Cannot await in the filter expression?

我正在关注此处的代码示例:https://docs.microsoft.com/en-us/aspnet/core/tutorials/first-web-api?view=aspnetcore-6.0&tabs=visual-studio-code

try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException) when (!TodoItemExists(id))
            {
                return NotFound();
            }

在我的代码中,方法 TodoItemExists(在我的例子中方法名称是 UserExists)是异步的。

代码示例:

try
{
    await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException) when (!await UserExists(id))
{
    return NotFound();
}

错误:无法在过滤器表达式中等待

正确的写法是什么?

“旧”方式:

try
{
    await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException) 
{
    if (!await UserExists(id)) 
      return NotFound();
    else   
      throw;
}

但在“找到用户但其他人先修改了它”的情况下,您可能想要 return 其他内容,以便客户端可以决定要做什么 - 甚至可能发送当前状态。在我看来 client/user 是合理的唯一可以解决“skip/overwrite/merge”选择的东西