//deleting connection between Hall and OpeningHours public bool RemoveHallOpeningHours(HallOpeningHours hallOpeningHours) { var hallOpeningHoursToRemove = Enumerable.FirstOrDefault <HallOpeningHours>(_hallRepository.HallOpeningHours(), o => o.OpeningHoursId == hallOpeningHours.OpeningHoursId && o.HallId == hallOpeningHours.HallId); //check if connection between Hall and OpeningHours exists in database if (hallOpeningHoursToRemove != null) { //check if Hall still has opening times int openingTimesCount = Enumerable.Count <HallOpeningHours>(_hallRepository.HallOpeningHours(), (h => h.HallId == hallOpeningHours.HallId)); //Hall needs to have at least 1 OpeningHours left after removing if (openingTimesCount > 1) { var hallOpeningHoursRemoved = _hallRepository.RemoveHallOpeningHours(hallOpeningHoursToRemove); //check if HallOpeningHours is removed if (hallOpeningHoursRemoved) { int openingHourCount = Enumerable.Count <HallOpeningHours>(_hallRepository.HallOpeningHours(), h => h.OpeningHoursId == hallOpeningHours.OpeningHoursId); //remove entry in OpeningHours table if it is no longer used if (openingHourCount == 0) { RemoveOpeningHours(new OpeningHours { OpeningHoursId = hallOpeningHours.OpeningHoursId }); } } return(hallOpeningHoursRemoved); } else { return(false); } } return(false); }