0

I am running into the following error whenever I am trying to update a row within a specific Table(Incidence)

Microsoft.EntityFrameworkCore.DbUpdateConcurrencyException: The database operation was expected to affect 1 row(s), but actually affected 0 row(s);

After debugging I realized that my ID RegistrationID is being passed as value 0 instead of the actual value in the db.

Code from Controller:

[HttpGet]
public async Task<IActionResult> EditTicket(int? RegistrationID)
{
    if (RegistrationID == null)
    {
        return StatusCode(400);
    }
    Incidence incidence = await _context.Incidence.FindAsync(RegistrationID);
    if (incidence == null)
    {
        return StatusCode(404);
    }
    return View(incidence);
}    
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> EditTicket(IList<IFormFile> Files, Incidence incidence)
{
    if (ModelState.IsValid)
    {
        if (string.IsNullOrEmpty(incidence.EmailAddress))
        {
            _context.Entry(incidence).Property(x1 => x1.DateServed).IsModified = false;
            _context.Entry(incidence).Property(x1 => x1.Source).IsModified = false;
            _context.Entry(incidence).State = EntityState.Modified;

            await _context.SaveChangesAsync();  // <= Exception occurs here
           ... 
        }
    }
}

I tried a solution from this link but did not work either.

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Nickson
  • 135
  • 12

0 Answers0