我正在關注這里的代碼示例: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();
}
錯誤:無法在過濾器運算式中等待
寫這個的正確方法是什么?
uj5u.com熱心網友回復:
“舊”方式:
try
{
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!await UserExists(id))
return NotFound();
else
throw;
}
但是您可能希望在“找到用戶但其他人先修改它”的情況下回傳其他內容,以便客戶端可以決定要做什么 - 甚至可以發送當前狀態。在我看來,客戶端/用戶是唯一可以解決“跳過/覆寫/合并”選擇的事情
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/406523.html
標籤:
