Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Error updating an entity: Conflict with Foreign Key

$
0
0

Hello!

On this action:

[HttpPost, ActionName("Test")]
    [ValidateAntiForgeryToken]
    public async Task<IActionResult> 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 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<Machine> Machines { get;set;}public virtual District Districts { get;set;}

I don't know why StoreID wont update. I'm not inserting a StoreID that does not exist in the table, since I choose them from a dropdownlist.

Thanks in advance for any help.


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>