Hello!
On this action:
[HttpPost, ActionName("Test")] [ValidateAntiForgeryToken] public async Task TestPost(Machine machinetoUpdate) { var machine = _context.Machines.Where(m => m.Id == machinetoUpdate.Id).FirstOrDefault(); machine.PUnit = machinetoUpdate.PUnit; machine.StoreID = machinetoUpdate.StoreID; machine.Status = machinetoUpdate.Status; _context.SaveChanges(); PopulateMachineTypeDropDownListStore(); return View(await _context.Machines.AsNoTracking().ToListAsync()); } I'm getting an error while trying to update StoreID.
[HttpPost, ActionName("Test")] [ValidateAntiForgeryToken] public async Task TestPost(Machine machinetoUpdate) { var machine = _context.Machines.Where(m => m.Id == machinetoUpdate.Id).FirstOrDefault(); machine.PUnit = machinetoUpdate.PUnit; machine.StoreID = machinetoUpdate.StoreID; machine.Status = machinetoUpdate.Status; _context.SaveChanges(); PopulateMachineTypeDropDownListStore(); return View(await _context.Machines.AsNoTracking().ToListAsync()); }
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:
public class Machine { public int Id { get; set; } public int TypeID { get; set; } public int SupplierID { get; set; } public int StoreID { get; set; } [StringLength(60)] [Display(Name = "Serial")] public string MchName { get; set; } [DataType(DataType.Date)] [DisplayFormat(DataFormatString ="{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] [Display(Name = "Fecha de Compra")] } This is the Store class:
public class Machine { public int Id { get; set; } public int TypeID { get; set; } public int SupplierID { get; set; } public int StoreID { get; set; } [StringLength(60)] [Display(Name = "Serial")] public string MchName { get; set; } [DataType(DataType.Date)] [DisplayFormat(DataFormatString ="{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] [Display(Name = "Fecha de Compra")] }
This is the Store class:
public class Store { [Key] public int StoreID { get; set; } public int DistrictID { get; set; } [Required] [StringLength(60)] public string StoreName { get; set; } [StringLength(60)] public string StoreAddress { get; set; } public virtual ICollection Machines { get; set; } public virtual District Districts { get; set; } }