Entity Framework - updating two different arrays of child associations 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 =&gt; 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 =&gt; s.ToolId == newTool.ToolId);
                if (match != null) {
                    existingTools.Remove(match);
                }
                else {
                    // add newTool to dbExercise
                    dbExercise.Tools.Add(dbTools.First(s =&gt; s.ToolId == newTool.ToolId));
                }
            }
            foreach (var existingTool in existingTools) {
                dbExercise.Tools.Remove(dbTools.First(s =&gt; 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 =&gt; s.MuscleId == newMuscle.MuscleId);
                if (match != null) {
                    existingMuscles.Remove(match);
                } else {
                    // add newMuscle to dbExercise
                    dbExercise.Muscles.Add(dbMuscles.First(s =&gt; s.MuscleId == newMuscle.MuscleId));
                }
            }
            foreach (var existingMuscle in existingMuscles) {
                dbExercise.Muscles.Remove(dbMuscles.First(s =&gt; s.MuscleId == existingMuscle.MuscleId));
            }


            db.ChangeTracker.Entries&lt;Tool&gt;().ToList().ForEach(p =&gt; p.State = EntityState.Unchanged);
            db.ChangeTracker.Entries&lt;Muscle&gt;().ToList().ForEach(p =&gt; p.State = EntityState.Unchanged);

            db.SaveChanges();
        }


Exercise: 

public class Exercise {

        public Exercise() {
            Tools = new Collection&lt;Tool&gt;();
            Muscles = new Collection&lt;Muscle&gt;();
        } 

        [Key]
        [Required]
        public int ExerciseId { get; set; }

        [StringLength(300, ErrorMessage = "The value cannot exceed 300 characters. ")]
        public string Title { get; set; }

        public virtual ICollection&lt;Tool&gt; Tools { get; set; }
        public virtual ICollection&lt;Muscle&gt; Muscles { get; set; }

    }
Use design patterns to simplfiy your Data Access Layer and the Separation of concerns.

This method UpdateExercise(Exercise exercise) is very complicated and is not SRP.

Your problem in the model architecture more than in code. Of course you can use some tricks to simplify the code syntax like LINQ queries but the core problem will still there!


I recommeded you to apply Repository pattern and UnitOfwork pattern then you will see how it easy can you do all wished changes in one Transaction.
Use eager loading to inclued the wished entities(Tools,Muscles, etc.)  and load all load all everywhere
Use Linq to simplify the queries
Use attached entities, if possible!! slower but better!