ASP.Net & EF Core 2 and Concurrency token error

asp.net-core-2.0 c# entity-framework-core

Question

Entity Framework Core 2, added a concurrency token property to model

[Timestamp]
public byte[] Timestamp { get; set; }

Controller Edit fails

        [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);
        }

the specific error I am receiving occurs when SaveChangesAsync occurs. catch pops and when I step in it goes straight to the throw.

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)

searching for the error message doesn't help. did find this article but it appears to be no help.

https://docs.microsoft.com/en-us/ef/core/saving/concurrency

1
2
9/15/2017 8:27:24 PM

Accepted Answer

as indicated in the comments, I was missing a hidden field to 'save' the timestamp in the view.

Followed this example: https://docs.microsoft.com/en-us/aspnet/core/data/ef-mvc/concurrency

for clarity added my altered Edit. I had to do something similar to Delete too. this needs to be added to the Edit view <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);
        }
2
9/15/2017 9:49:38 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow