title: Saving changes to child collections with Entity Framework
body: <p>I have made changes to an EntityCollection which is an object of another EntityCollection,
  and when I try to save those changes, I get the following error:</p>
  
  <blockquote>
    <p>The EntityCollection has already been initialized. The
    InitializeRelatedCollection method should only be called to initialize
    a new EntityCollection during deserialization of an object graph.</p>
  </blockquote>
  
  <p>Any ideas what I might be missing?</p>
  
  <p>The following is the code I used.</p>
  
  <pre><code>public void UpdateCompanyManagement(Company newCompany)
  {
      Company oldCompany = entities.Companies.Where(c =&gt; c.COM_ID == newCompany.COM_ID).SingleOrDefault();
  
      oldCompany.Managements = newCompany.Managements;
  
      try
      {
          entities.SaveChanges();
      }
      catch (OptimisticConcurrencyException)
      {
          entities.Refresh(RefreshMode.ClientWins, newCompany.Managements);
          entities.SaveChanges();
      }
  }
  </code></pre>
  
  <p>I'm able to save the parent collection successfully, using the following code:</p>
  
  <pre><code>public void UpdateCompanyDetails(Company newCompany)
  {
      Company oldCompany = entities.Companies.Where(c =&gt; c.COM_ID == newCompany.COM_ID).SingleOrDefault();
      entities.ObjectStateManager.ChangeObjectState(oldCompany.city, System.Data.EntityState.Modified);
      oldCompany = newCompany;
  
      try
      {
          entities.SaveChanges();
      }
      catch (OptimisticConcurrencyException)
      {
          entities.Refresh(RefreshMode.ClientWins, oldCompany);
          entities.SaveChanges();
      }
  }
  </code></pre>