Error updating an entity: Conflict with Foreign Key

Jun 4 2017 11:59 PM

Hello!

On this action:

  1. [HttpPost, ActionName("Test")]  
  2.     [ValidateAntiForgeryToken]  
  3.     public async Task TestPost(Machine machinetoUpdate)  
  4.     {  
  5.         var machine = _context.Machines.Where(m => m.Id == machinetoUpdate.Id).FirstOrDefault();  
  6.   
  7.         machine.PUnit = machinetoUpdate.PUnit;  
  8.         machine.StoreID = machinetoUpdate.StoreID;  
  9.         machine.Status = machinetoUpdate.Status;  
  10.         _context.SaveChanges();  
  11.         PopulateMachineTypeDropDownListStore();  
  12.   
  13.         return View(await _context.Machines.AsNoTracking().ToListAsync());  
  14.     }  

I'm getting an error while trying to update StoreID.

SqlException: The UPDATE statement conflicted with the FOREIGN KEY constraint "FK_Machine_Store_StoreID". The conflict occurred in database "Application4", table "dbo.Store", column 'StoreID'. The statement has been terminated.

StoreID is a foreign key for the Machine class:

  1. public class Machine  
  2. {  
  3.     public int Id { getset; }  
  4.     public int TypeID { getset; }  
  5.     public int SupplierID { getset; }  
  6.     public int StoreID { getset; }  
  7.     [StringLength(60)]  
  8.     [Display(Name = "Serial")]  
  9.     public string MchName { getset; }  
  10.     [DataType(DataType.Date)]  
  11.     [DisplayFormat(DataFormatString ="{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]  
  12.     [Display(Name = "Fecha de Compra")]  
  13.     }  

This is the Store class:
  1. public class Store  
  2. {  
  3.     [Key]  
  4.     public int StoreID { getset; }  
  5.     public int DistrictID { getset; }  
  6.     [Required]  
  7.     [StringLength(60)]  
  8.     public string StoreName { getset; }  
  9.     [StringLength(60)]  
  10.     public string StoreAddress { getset; }  
  11.   
  12.     public virtual ICollection Machines { getset; }  
  13.     public virtual District Districts { getset; }  
  14. }   
I don't know why StoreID won't update. I'm not inserting a StoreID that does not exist in the Store model, since I choose it from a dropdownlist.
 
This is  the view:
 
  1. @foreach (var item in Model)  
  2. {  
  3.     <form asp-action="Test" asp-route-id="@item.Id">  
  4.         <table class="table">  
  5.             <tbody>  
  6.                 <tr>  
  7.                     <td>  
  8.                         <input type="hidden" asp-for="@item.Id" />  
  9.                         <div class="form-group">  
  10.                             <div class="col-md-10">  
  11.                                 <input asp-for="@item.MchName" readonly class="form-control" />  
  12.                                 <span asp-validation-for="@item.MchName" class="text-danger"></span>  
  13.                             </div>  
  14.                         </div>  
  15.                     </td>  
  16.                     <td>  
  17.                         <div class="form-group">  
  18.                             <div class="col-md-10">  
  19.                                 <select asp-for="@item.StoreID" class="form-control" asp-items="ViewBag.StoreID">  
  20.                                 </select>  
  21.                                 <span asp-validation-for="@item.StoreID" class="text-danger"></span>  
  22.                             </div>  
  23.                         </div>  
  24.                     </td>  
  25.                     <td>  
  26.                         <div class="form-group">  
  27.                             <div class="col-md-10">  
  28.                                 <input type="number" max="10" step=".1" asp-for="@item.PUnit" class="form-control" />  
  29.                                 <span asp-validation-for="@item.PUnit" class="text-danger"></span>  
  30.                             </div>  
  31.                         </div>  
  32.                     </td>  
  33.                     <td>  
  34.                         <div class="form-group">  
  35.                             <div class="col-md-10">  
  36.                                 <select name="Status" asp-for="@item.Status" class="form-control">  
  37.                                     <option value="0">Operativo</option>  
  38.                                     <option value="1">Nuevo Item</option>  
  39.                                     <option value="2">Reparación</option>  
  40.                                 </select>  
  41.                                 <span asp-validation-for="@item.Status" class="text-danger"></span>  
  42.                             </div>  
  43.                         </div>  
  44.                     </td>  
  45.                     <td>  
  46.                         <input type="submit" value="Update" class="btn btn-default" />  
  47.                     </td>  
  48.                 </tr>  
  49.             </tbody>  
  50.         </table>  
  51.   
  52.     </form>}  
 
 
I don't know if the dropdownlist for the Store might be sending a StoreID value wrong. It seems it's ok, so I don't know.
 
Thanks in advance for any help.
 
Update: More Info 
 
I believe my problem could be on my Get Method.
 
My view (Test.cshtml) is correctly loading and showing the info of all the items in the Machine class in the table, including the StoreID, however, my Get Method is this:
 
  1. public async Task Test()  
  2.         {  
  3.             PopulateMachineTypeDropDownListStore();  
  4.             return View(await _context.Machines.AsNoTracking().ToListAsync());  
  5.               
  6.         }  
Is it possible that it does not matter if my View is showing what I want because my Get Method is not capturing any info on the StoreID? And thus, when I hit the 'Update' button it might be trying to duplicate the StoreID and for that I get the error?

Answers (4)