osyris zosar

osyris zosar

  • NA
  • 289
  • 27.6k

Microsoft.EntityFrameworkCore.Database.Transaction: Error

Apr 15 2021 1:37 PM
View:
  1. @model IEnumerable<Webshop.Models.Categorys>    
  2.     
  3. <ul>    
  4. @foreach(var item in Model)    
  5.     {    
  6.     <li>@item.category      
  7.     <a asp-action="DeleteCategory" asp-route-id="@item.Id">Delete</a></li>    
  8.     }    
  9. </ul>  
Controller:
  1. public async Task<IActionResult> DeleteCategory(int Id)    
  2. {    
  3.     if (Id == null)    
  4.     {    
  5.         return NotFound();    
  6.     }    
  7.     var findId = _context.Categorys.FirstOrDefault(x => x.Id == Id);    
  8.      _context.Categorys.Remove(findId);    
  9.     _context.SaveChangesAsync();  
  10.     return RedirectToAction(nameof(Category));    
  11. }    
I have to press several times on the Delete link in order for it to work
 
when it doesnt work i get the error:
 
Microsoft.EntityFrameworkCore.Database.Transaction: Error: An error occurred using a transaction.
 
I would like to Understand why the code is not running smoothly
 
Thanks

Answers (3)