public static void DeleteTraining(int id) { using (var ctx = new DFAppEntities()) { DBModel.Trainings t = ctx.Trainings.FirstOrDefault(x => x.TrainingID == id); if (t != null) { ctx.Entry(t).Collection(x => x.TrainingMemberPresenceRegistrations).Load(); for (int i = t.TrainingMemberPresenceRegistrations.Count() - 1; i >= 0; i--) { ctx.TrainingMemberPresenceRegistrations.Remove(t.TrainingMemberPresenceRegistrations.ElementAt(i)); } ctx.Trainings.Remove(t); ctx.SaveChanges(); } } }
public static void DeleteDanceGroup(int id) { using (var ctx = new DFAppEntities()) { DBModel.DanceGroups t = ctx.DanceGroups.FirstOrDefault(x => x.DanceGroupID == id); if (t != null) { ctx.Entry(t).Collection(x => x.DanceGroupChoreographies).Load(); ctx.Entry(t).Collection(x => x.DanceGroupMembers).Load(); ctx.Entry(t).Collection(x => x.DanceGroupStaff).Load(); ctx.Entry(t).Collection(x => x.DanceSelectionDanceGroups).Load(); ctx.Entry(t).Collection(x => x.DanceGroupLocations).Load(); ctx.Entry(t).Collection(x => x.UserDanceGroups).Load(); for (int i = t.DanceGroupChoreographies.Count() - 1; i >= 0; i--) { ctx.DanceGroupChoreographies.Remove(t.DanceGroupChoreographies.ElementAt(i)); } for (int i = t.DanceGroupMembers.Count() - 1; i >= 0; i--) { ctx.DanceGroupMembers.Remove(t.DanceGroupMembers.ElementAt(i)); } for (int i = t.DanceGroupStaff.Count() - 1; i >= 0; i--) { ctx.DanceGroupStaff.Remove(t.DanceGroupStaff.ElementAt(i)); } for (int i = t.DanceSelectionDanceGroups.Count() - 1; i >= 0; i--) { ctx.DanceSelectionDanceGroups.Remove(t.DanceSelectionDanceGroups.ElementAt(i)); } for (int i = t.DanceGroupLocations.Count() - 1; i >= 0; i--) { ctx.DanceGroupLocations.Remove(t.DanceGroupLocations.ElementAt(i)); } for (int i = t.UserDanceGroups.Count() - 1; i >= 0; i--) { ctx.UserDanceGroups.Remove(t.UserDanceGroups.ElementAt(i)); } ctx.DanceGroups.Remove(t); ctx.SaveChanges(); } } }