ASP.Net & EF Core 2 和并发令牌错误
ASP.Net & EF Core 2 and Concurrency token error
Entity Framework 核心 2,向模型
添加了并发令牌 属性
[Timestamp]
public byte[] Timestamp { get; set; }
控制器编辑失败
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, [Bind("Id,Name,Description,IsDeleted,ParentId")] ItemStatus itemStatus)
{
if (id != itemStatus.Id)
return NotFound();
if (ModelState.IsValid)
{
try
{
_context.Update(itemStatus);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ItemStatusExists(itemStatus.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["ParentId"] = new SelectList(_context.ItemStatus, "Id", "Description", itemStatus.ParentId);
return View(itemStatus);
}
我收到的特定错误发生在 SaveChangesAsync 发生时。抓住流行音乐,当我介入时,它会直接投掷。
DbUpdateConcurrencyException: Database operation expected to affect 1
row(s) but actually affected 0 row(s). Data may have been modified or
deleted since entities were loaded. See
http://go.microsoft.com/fwlink/?LinkId=527962 for information on
understanding and handling optimistic concurrency exceptions.
Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(int
commandIndex, int expectedRowsAffected, int rowsAffected)
搜索错误消息没有帮助。确实找到了这篇文章,但似乎没有帮助。
如评论中所述,我缺少 'save' 视图中时间戳的隐藏字段。
遵循这个例子:https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/concurrency
为了清楚起见,添加了我修改后的编辑。我也必须做一些类似于删除的事情。这需要添加到编辑视图 <input type="hidden" asp-for="Timestamp" />
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, [Bind("Id,Name,Description,ParentId,Timestamp")] ItemStatus itemStatus)
{
if (id != itemStatus.Id)
return NotFound();
if (ModelState.IsValid)
{
try
{
_context.Update(itemStatus);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ItemStatusExists(itemStatus.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["ParentId"] = new SelectList(_context.ItemStatus, "Id", "Description", itemStatus.ParentId);
return View(itemStatus);
}
Entity Framework 核心 2,向模型
添加了并发令牌 属性[Timestamp]
public byte[] Timestamp { get; set; }
控制器编辑失败
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, [Bind("Id,Name,Description,IsDeleted,ParentId")] ItemStatus itemStatus)
{
if (id != itemStatus.Id)
return NotFound();
if (ModelState.IsValid)
{
try
{
_context.Update(itemStatus);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ItemStatusExists(itemStatus.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["ParentId"] = new SelectList(_context.ItemStatus, "Id", "Description", itemStatus.ParentId);
return View(itemStatus);
}
我收到的特定错误发生在 SaveChangesAsync 发生时。抓住流行音乐,当我介入时,它会直接投掷。
DbUpdateConcurrencyException: Database operation expected to affect 1 row(s) but actually affected 0 row(s). Data may have been modified or deleted since entities were loaded. See http://go.microsoft.com/fwlink/?LinkId=527962 for information on understanding and handling optimistic concurrency exceptions.
Microsoft.EntityFrameworkCore.Update.AffectedCountModificationCommandBatch.ThrowAggregateUpdateConcurrencyException(int commandIndex, int expectedRowsAffected, int rowsAffected)
搜索错误消息没有帮助。确实找到了这篇文章,但似乎没有帮助。
如评论中所述,我缺少 'save' 视图中时间戳的隐藏字段。
遵循这个例子:https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/concurrency
为了清楚起见,添加了我修改后的编辑。我也必须做一些类似于删除的事情。这需要添加到编辑视图 <input type="hidden" asp-for="Timestamp" />
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(Guid id, [Bind("Id,Name,Description,ParentId,Timestamp")] ItemStatus itemStatus)
{
if (id != itemStatus.Id)
return NotFound();
if (ModelState.IsValid)
{
try
{
_context.Update(itemStatus);
await _context.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ItemStatusExists(itemStatus.Id))
{
return NotFound();
}
else
{
throw;
}
}
return RedirectToAction(nameof(Index));
}
ViewData["ParentId"] = new SelectList(_context.ItemStatus, "Id", "Description", itemStatus.ParentId);
return View(itemStatus);
}