public void Delete(int id) { Customer c = db.Customer.Find(id); db.Remove(c); db.SaveChanges(); }
public ActionResult Delete(int manuId) { var toDelete = _db.Manufacturers.FirstOrDefault(manu => manu.ManufacturerId == manuId); if (toDelete == null) { return(RedirectToAction("")); } //see if any devices are connected and if so delete them all from the database if (_db.DeviceModels.Any(mod => mod.ManufacturerId == manuId)) { var connectedModels = _db.DeviceModels.Where(mod => mod.ManufacturerId == manuId); foreach (var connected in connectedModels) { _db.Remove(connected); } } _db.Remove(toDelete); _db.SaveChanges(); return(RedirectToAction("")); }
public IActionResult Delete(long id) { //make sure customer with id exists var requested = _db.Customers.FirstOrDefault(c => c.CustomerId == id); if (requested == null) { return(RedirectToAction("List")); } //check if any associated emails exist, and if so remove from db if (_db.Emails.Any(e => e.CustomerId == id)) { var linkedEmails = _db.Emails.Where(e => e.CustomerId == id); foreach (var linkedEmail in linkedEmails) { _db.Remove(linkedEmail); } } //check if any associated phonenumbers exist, and if so remove from db if (_db.PhoneNumbers.Any(p => p.CustomerId == id)) { var linkedPhones = _db.PhoneNumbers.Where(p => p.CustomerId == id); foreach (var linkedPhone in linkedPhones) { _db.Remove(linkedPhone); } } //check if any associated addresses exist, and if so remove from db if (_db.MailingAddresses.Any(m => m.CustomerId == id)) { var linkedAddresses = _db.MailingAddresses.Where(m => m.CustomerId == id); foreach (var linkedAddress in linkedAddresses) { _db.Remove(linkedAddress); } } //check if there are any associated Work Orders and if so delete all devices linked to //that order first then delete the order if (_db.WorkOrders.Any(o => o.CustomerId == id)) { var linkedOrders = _db.WorkOrders.Where(o => o.CustomerId == id); foreach (var order in linkedOrders) { var linkedDevices = _db.Devices.Where(d => d.WorkOrderNumber == order.WorkOrderNumber); foreach (var device in linkedDevices) { _db.Remove(device); } _db.Remove(order); } } //remove customer form db and save changes _db.Remove(requested); _db.SaveChanges(); return(RedirectToAction("List")); }