public async Task <IActionResult> DeleteConfirmed(int id) { var course = await _courseContext.Course.FindAsync(id); var locations = _locationContext.Location.Where(l => l.CourseId == course.CourseId); foreach (var location in locations) { var questionsToLocations = _questionContext.Question.Where(q => q.LocationId == location.LocationId); foreach (var question in questionsToLocations) { var answersToQuestion = _answerContext.Answer.Where(ans => ans.QuestionId == question.QuestionId); _answerContext.Answer.RemoveRange(answersToQuestion); _questionContext.Remove(question); } _locationContext.Location.Remove(location); } _courseContext.Course.Remove(course); await _answerContext.SaveChangesAsync(); await _questionContext.SaveChangesAsync(); await _locationContext.SaveChangesAsync(); await _courseContext.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); }
public async Task <IActionResult> DeleteConfirmed(int id) { var question = await _questionContext.Question.FindAsync(id); var answersToQuestion = _answerContext.Answer.Where(ans => ans.QuestionId == question.QuestionId); _answerContext.Answer.RemoveRange(answersToQuestion); await _answerContext.SaveChangesAsync(); _questionContext.Remove(question); await _questionContext.SaveChangesAsync(); var location = _locationContext.Location.Find(question.LocationId); if (location == null) { return(NotFound()); } return(RedirectToAction("Details", "Courses", new { id = location.CourseId })); }
public async Task <IActionResult> DeleteConfirmed(int id) { var location = await _locationContext.Location.FindAsync(id); var questionsToLocations = _questionContext.Question.Where(q => q.LocationId == location.LocationId); foreach (var question in questionsToLocations) { var answersToQuestion = _answerContext.Answer.Where(ans => ans.QuestionId == question.QuestionId); _answerContext.Answer.RemoveRange(answersToQuestion); _questionContext.Remove(question); } _locationContext.Location.Remove(location); await _answerContext.SaveChangesAsync(); await _questionContext.SaveChangesAsync(); await _locationContext.SaveChangesAsync(); return(RedirectToAction("Details", "Courses", new { id = location.CourseId })); }