title: MVC 4 and lazy loading: "Collection was modified" error when deleting multiple records
body: <p>In ASP.NET MVC 4 project, I have a model <code>LeagueMember</code>:</p>
  
  <pre><code>public class LeagueMember
  {
      [Key, Column(Order = 0)]
      public int MemberId { get; set; }
  
      [Key, Column(Order = 1)]
      public int LeagueId { get; set; }
  
      public bool? IsActive { get; set; }
  
      public virtual League League { get; set; }
  
      public virtual Member Member { get; set; }
  
  }
  </code></pre>
  
  <p>In League model, I have:</p>
  
  <pre><code>public virtual ICollection&lt;LeagueMember&gt; LeagueMembers { get; set; }
  </code></pre>
  
  <p>In the <code>Edit</code> view of <code>League</code> controller, I have a dropdown for <code>LeagueMembers</code> and in its Edit action I am trying to delete existing LeagueMembers and creating new ones:</p>
  
  <pre><code>[HttpPost]
  public ActionResult Edit(LeagueMembersViewModel leagueMemberViewModel)
  {
      if (ModelState.IsValid)
      {
          var league = leagueMemberViewModel.League;
          context.Entry(league).State = EntityState.Modified;
  
          /* without this line the next foreach statement would complain that 
             League.LeagueMembers is null due to lazy loading..*/
          context.Entry(league).Collection("LeagueMembers").Load();
  
          var leagueMembers = leagueMemberViewModel.League.LeagueMembers;
  
          foreach (var leagueMember in leagueMembers)
          {
              context.LeagueMembers.Remove(leagueMember);
          }
  
          foreach (var memberId in leagueMemberViewModel.SelectedMembers)
          {
              var leagueMember = new LeagueMember { MemberId = memberId, League = league };
              context.LeagueMembers.Add(leagueMember);
          }
          context.SaveChanges();
  
          return RedirectToAction("Index");
     }
  
      leagueMemberViewModel.MemberList = new MultiSelectList(context.Members, "Id", "Name", leagueMemberViewModel.SelectedMembers);
      return View(leagueMemberViewModel);
  }
  </code></pre>
  
  <p>When executed, the line <code>foreach (var leagueMember in leagueMembers)</code> complains on second iteration of forloop:</p>
  
  <blockquote>
    <p>Collection was modified; enumeration operation may not execute</p>
  </blockquote>
  
  <p>Is there a better way to delete bulk records in a one-to-many relationship?</p>
  
  <p>In this case, <em>to Remove All <code>leagueMemberViewModel.League.LeagueMembers</code></em> perhaps without iterating through the collection?</p>
  
  <p>Something like, replacing:</p>
  
  <pre><code>context.Entry(league).Collection("LeagueMembers").Load();
  
  var leagueMembers = leagueMemberViewModel.League.LeagueMembers;
  foreach (var leagueMember in leagueMembers)
  {
      context.LeagueMembers.Remove(leagueMember);
  }
  </code></pre>
  
  <p>with:</p>
  
  <pre><code> leagueMember.League.LeagueMembers.Remove();
  </code></pre>