Exercises have Muscles and Tools.  When someone adds or updates an Exercise, they also send an array of Tools and Muscles.  Adds are pretty straightforward, but my update method feels a bit bulky.  How can I simplify this?public void UpdateExercise(Exercise exercise) {            var dbExercise = db.Exercises.First(s => s.ExerciseId == exercise.ExerciseId);            var dbTools = db.Tools.ToList();            var dbMuscles = db.Muscles.ToList();            dbExercise.Title = exercise.Title;            // handle tools            var existingTools = dbExercise.Tools.ToList();            foreach (var newTool in exercise.Tools) {                // handle pre-existing                var match = existingTools.FirstOrDefault(s => s.ToolId == newTool.ToolId);                if (match != null) {                    existingTools.Remove(match);                }                else {                    // add newTool to dbExercise                    dbExercise.Tools.Add(dbTools.First(s => s.ToolId == newTool.ToolId));                }            }            foreach (var existingTool in existingTools) {                dbExercise.Tools.Remove(dbTools.First(s => s.ToolId == existingTool.ToolId));            }            // handle exercises            var existingMuscles = dbExercise.Muscles.ToList();            foreach (var newMuscle in exercise.Muscles) {                // handle pre-existing                var match = existingMuscles.FirstOrDefault(s => s.MuscleId == newMuscle.MuscleId);                if (match != null) {                    existingMuscles.Remove(match);                } else {                    // add newMuscle to dbExercise                    dbExercise.Muscles.Add(dbMuscles.First(s => s.MuscleId == newMuscle.MuscleId));                }            }            foreach (var existingMuscle in existingMuscles) {                dbExercise.Muscles.Remove(dbMuscles.First(s => s.MuscleId == existingMuscle.MuscleId));            }            db.ChangeTracker.Entries<Tool>().ToList().ForEach(p => p.State = EntityState.Unchanged);            db.ChangeTracker.Entries<Muscle>().ToList().ForEach(p => p.State = EntityState.Unchanged);            db.SaveChanges();        }Exercise: public class Exercise {        public Exercise() {            Tools = new Collection<Tool>();            Muscles = new Collection<Muscle>();        }         [Key]        [Required]        public int ExerciseId { get; set; }        [StringLength(300, ErrorMessage = The value cannot exceed 300 characters. )]        public string Title { get; set; }        public virtual ICollection<Tool> Tools { get; set; }        public virtual ICollection<Muscle> Muscles { get; set; }    }
Entity Framework - updating two different arrays of child associations
c#;entity framework