public ActionResult Delete(string id, School school) { var schoolToDelete = _schoolRepository.GetNullableById(id); if (schoolToDelete == null) { return(RedirectToAction("Index")); } _schoolRepository.Remove(schoolToDelete); Message = "School Removed Successfully"; return(RedirectToAction("Index")); }
public ActionResult Delete(string id, Employee employee) { var employeeToDelete = _employeeRepository.GetNullableById(id); if (employeeToDelete == null) { return(RedirectToAction("Index")); } _employeeRepository.Remove(employeeToDelete); Message = "Employee Removed Successfully"; return(RedirectToAction("Index")); }
public virtual void CanDeleteEntity() { if (typeof(IdT) == typeof(int)) { var count = _intRepository.GetAll().ToList().Count(); var foundEntity = _intRepository.GetAll().ToList()[2]; //Update and commit entity _intRepository.DbContext.BeginTransaction(); _intRepository.Remove(foundEntity); _intRepository.DbContext.CommitTransaction(); Assert.AreEqual(count - 1, _intRepository.GetAll().ToList().Count()); foundEntity = Repository.OfType <T>().GetNullableById(3); Assert.IsNull(foundEntity); } else if (typeof(IdT) == typeof(Guid)) { var count = _guidRepository.GetAll().ToList().Count(); var foundEntity = _guidRepository.GetAll().ToList()[2]; //Update and commit entity _guidRepository.DbContext.BeginTransaction(); _guidRepository.Remove(foundEntity); _guidRepository.DbContext.CommitTransaction(); Assert.AreEqual(count - 1, _guidRepository.GetAll().ToList().Count()); foundEntity = _guidRepository.GetNullableById(SpecificGuid.GetGuid(3)); Assert.IsNull(foundEntity); } else { var count = _stringRepository.GetAll().ToList().Count(); var foundEntity = _stringRepository.GetAll().ToList()[2]; //Update and commit entity _stringRepository.DbContext.BeginTransaction(); _stringRepository.Remove(foundEntity); _stringRepository.DbContext.CommitTransaction(); Assert.AreEqual(count - 1, _stringRepository.GetAll().ToList().Count()); foundEntity = _stringRepository.GetNullableById("3"); Assert.IsNull(foundEntity); } }
public async Task <IActionResult> Delete(string id) { var country = await _countryRepository.Query().FirstOrDefaultAsync(x => x.Id == id); if (country == null) { return(NotFound()); } try { _countryRepository.Remove(country); await _countryRepository.SaveChangesAsync(); } catch (DbUpdateException) { return(BadRequest(new { Error = $"The country {country.Name} can't not be deleted because it is referenced by other tables" })); } return(NoContent()); }