public static void EditPlans(List <Plan> newPlans) { using (CalendarDBContext db = new CalendarDBContext()) { foreach (Plan plan in Plans) { if (newPlans.FindAll(p => p.Id == plan.Id).Count == 0) { if (db.Plans.Where(p => p.Id == plan.Id).First().Users.Count <= 1) { db.Plans.Remove(db.Plans.Where(p => p.Id == plan.Id).First()); } db.Users.Where(u => u.Id == ActiveUser.Id).First().Plans.Remove(db.Plans.Where(p => p.Id == plan.Id).First()); } } foreach (Plan plan in newPlans) { if (Plans.FindAll(p => p.Id == plan.Id).Count == 0) { db.Plans.Add(plan); db.Users.Where(u => u.Id == ActiveUser.Id).First().Plans.Add(plan); } } db.SaveChanges(); } }