title: EF Change Tracker: Collection was modified; enumeration operation may not execute
body: <p>I'm getting an error when I loop through a collection. I'm not changing the list at any point, but it gives me the following "Collection was modified; enumeration operation may not execute. Following code shows the method that I have this Parallel.Foreach.</p>
  
  <pre><code>public void DownloadImages()
          {
              IList&lt;Vehicle&gt; vehicles = _repository.Retrieve().ToList();
              Parallel.ForEach(vehicles, vehicle =&gt;
              {
                  IList&lt;VehiclePhoto&gt; vehiclePhotos =
                      vehicle.VehiclePhotos.Where(x =&gt; x.ImageStatus == ImageStatus.Inactive).ToList();
                  if (vehiclePhotos.Count &gt; 0)
                  {
                      Parallel.ForEach(vehiclePhotos, photo =&gt;
                      {
                          using (var client = new WebClient())
                          {
                              try
                              {
                                  string fileName = Guid.NewGuid() + ".png";
                                  string filePath = _imagePath + "\\" + fileName;
                                  client.DownloadFile(photo.ImageUrl, filePath);
                                  photo.FileName = fileName;
                                  photo.ImageStatus = ImageStatus.Active;
                              }
                              catch (Exception)
                              {
                              }
                          }
                      });
  
                      _repository.Save(vehicle);
                  }
              });
          }
  </code></pre>
  
  <p>This happens when _repository.Save(vehicle) is called. Following code will show the save changes method. base.SaveChanges(); is the place where error get raised.</p>
  
  <pre><code>public override int SaveChanges()
          {
              DateTime nowAuditDate = DateTime.Now;
              IEnumerable&lt;System.Data.Entity.Infrastructure.DbEntityEntry&lt;DomainEntity&gt;&gt; changeSet = ChangeTracker.Entries&lt;DomainEntity&gt;();
              if (changeSet != null)
              {
                  foreach (System.Data.Entity.Infrastructure.DbEntityEntry&lt;DomainEntity&gt; entry in changeSet)
                  {
                      switch (entry.State)
                      {
                          case EntityState.Added:
                              entry.Entity.Created = nowAuditDate;
                              entry.Entity.Modified = nowAuditDate;
                              break;
                          case EntityState.Modified:
                              entry.Entity.Modified = nowAuditDate;
                              break;
                      }
                  }
              }
  
              return base.SaveChanges();
          }
  </code></pre>
  
  <p>Any ideas on this?</p>
  
  <p><strong>EDITED:</strong>
  I was trying to fix this above mentioned error and changed few code lines in DownloadImages method. Those changes are as follows:</p>
  
  <p>Instead of </p>
  
  <pre><code>IList&lt;Vehicle&gt; vehicles = _repository.Retrieve().ToList(); 
  </code></pre>
  
  <p>I used var </p>
  
  <pre><code>var vehicles = _repository.Retrieve().AsParallel(); 
  </code></pre>
  
  <p>Instead of </p>
  
  <pre><code>IList&lt;VehiclePhoto&gt; vehiclePhotos =
                          vehicle.VehiclePhotos.Where(x =&gt; x.ImageStatus == ImageStatus.Inactive).ToList();
  </code></pre>
  
  <p>I used var </p>
  
  <pre><code>var  vehiclePhotos =
                              vehicle.VehiclePhotos.Where(x =&gt; x.ImageStatus == ImageStatus.Inactive).AsParallel();
  </code></pre>
  
  <p>When I tried to run the code again. It gave me a different error: Error is as follows:</p>
  
  <p>In the header it says </p>
  
  <pre><code>System.Data.Entity.Infrastructure.DbUpdateException
  </code></pre>
  
  <p>But in the innerException</p>
  
  <pre><code>System.Data.Entity.Core.UpdateException
  </code></pre>
  
  <p><strong>ExecuteNonQuery requires the command to have a transaction when the connection assigned to the command is in a pending local transaction.  The Transaction property of the command has not been initialized.</strong></p>