title: EF 4.1 and "Collection was modified; enumeration operation may not execute." exception
body: <p>This has been driving me nuts for the last 2 days.
  I have 3 pretty basic classes (well, reduced for readability)</p>
  
  <pre><code>public class Employee 
  {
      public string Name { set; get; }
      virtual public Employer Employer { set; get; }
  
      public Employee(string name)
      {
          this.Name = name;
      }
  }
  </code></pre>
  
  <p><strong>,</strong></p>
  
  <pre><code>// this basically ties Employee and his role in a company.
  public class EmployeeRole{
      public int Id { set; get; }
      virtual public Employee Employee { set; get; }
      public string Role { set; get; }
  
      public EmployeeRole(Employee employee, string role){
          this.Employee = employee;
          this.Role = role;
      }
  }
  </code></pre>
  
  <p><strong>and</strong></p>
  
  <pre><code>public class Employer{
  
      public string Name { set; get; }
      List&lt;EmployeeRole&gt; employees = new List&lt;EmployeeRole&gt;();
      virtual public List&lt;EmployeeRole&gt; Employees { get { return this.employees; } }
  
      public Employer(string name, Employee creator){
          this.Name = name;
          this.Employees.Add(new EmployeeRole(creator, "Creator"));
          creator.Employer = this;
      }
  }
  </code></pre>
  
  <p>Seems pretty simple. Not doing any specific configuration for those classes in DbContext.</p>
  
  <p>But, when I run following code</p>
  
  <pre><code>using (DbContext db = DbContext.GetNewDbContext()){
  
      Employee creator = new Employee("Bob");
      db.Employees.Add(creator);
      db.SaveChanges();
  
      Employer employer = new Employer("employer", creator);
      db.Employers.Add(employer);
      db.SaveChanges();
     // I know I can call SaveChanges once (and it actually works in this case), 
     // but I want to make sure this would work with saved entities.
  }
  </code></pre>
  
  <p>It throws following exception:</p>
  
  <blockquote>
    <p>Collection was modified; enumeration operation may not execute.</p>
  </blockquote>
  
  <p>Stack trace:    </p>
  
  <blockquote>
    <p>at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource
    resource)    at
    System.Collections.Generic.List<code>1.Enumerator.MoveNextRare()    at
    System.Collections.Generic.List</code>1.Enumerator.MoveNext()    at
    System.Data.Objects.ObjectStateManager.PerformAdd(IList<code>1 entries)<br>
    at
    System.Data.Objects.ObjectStateManager.AlignChangesInRelationships(IList</code>1
    entries)    at System.Data.Objects.ObjectStateManager.DetectChanges() 
    at System.Data.Objects.ObjectContext.DetectChanges()    at
    System.Data.Entity.Internal.InternalContext.DetectChanges(Boolean
    force)    at
    System.Data.Entity.Internal.Linq.InternalSet<code>1.ActOnSet(Action action,
    EntityState newState, Object entity, String methodName)    at
    System.Data.Entity.Internal.Linq.InternalSet</code>1.Add(Object entity)<br>
    at System.Data.Entity.DbSet`1.Add(TEntity entity)</p>
  </blockquote>
  
  <p>Anyone has an idea what's going on and maybe how to fix it ?
  Thanks !</p>