I have a create form where if the specific Medicine exist, its number of supply will update or added with the new entry however if the specific Medicine doesn't exist, it will create a new batch of data.
Im having trouble at understanding how update works in MVC.
Here is the error:
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded.
Here is my controller:
public ActionResult Create([Bind(Include = "SupplyID,MedicineID,Expiration,NumberOfSupply")] Supply supply)
    {
        if (ModelState.IsValid)
        {
            bool supplyExsist = db.Supplies.Any(x => x.Expiration == supply.Expiration && x.MedicineID == supply.MedicineID);
            if (supplyExsist)
            {
                var currentSupply = (from x in db.Supplies //get current supply
                                     where x.MedicineID == supply.MedicineID
                                     && x.Expiration == supply.Expiration
                                     select x.NumberOfSupply).First();
                db.Entry(supply).State = EntityState.Modified;
                supply.NumberOfSupply = currentSupply + supply.NumberOfSupply;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                db.Supplies.Add(supply);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
        }
        ViewBag.MedicineID = new SelectList(db.Medicines, "MedicineID", "MedicineName", supply.MedicineID);
        return View(supply);
    }
Model:
public class Supply
{
    [Key]
    public int SupplyID { get; set; }
    [ForeignKey("Medicine")]
    public int MedicineID { get; set; }
    public Medicine Medicine { get; set; }
    [DataType(DataType.Date)]
    public DateTime Expiration { get; set; }
    [Display(Name = "Quantity")]
    [Range(1, int.MaxValue, ErrorMessage = "The value must be greater than 0")]
    public int NumberOfSupply { get; set; }
}
 
     
    