title: Entity Framework - how to merge child collections? (Best Practices)
body: <p>Following is the exact scenario in my application.</p>
  
  <p>There are two entities:</p>
  
  <ul>
  <li>Customer: CustomerId, CustomerName</li>
  <li>CustomerAddress: AddressId, CustomerId (FK), Area, City</li>
  </ul>
  
  <p>A customer has one-to-many relation with CustomerAddress.
  A customer can have addresses of more than one cities.</p>
  
  <p>When I save changes to the existing customer - at that time, its underlying CustomerAddress collection will be having all addresses of <strong>one particular city</strong> only. </p>
  
  <p>So when I update that customer in the database, it should -</p>
  
  <ul>
  <li>Add all new addresses of that city for the customer (i.e. the one exists in collection but not in db for that customer)</li>
  <li>Retain those addresses which are common in both</li>
  <li>Delete all those addresses which exist in database but not in collection for that city.</li>
  </ul>
  
  <p>I know one method, wherein I can fetch Customer entity from database, and can add, delete, retain by looping through Addresses collection there. </p>
  
  <p>But I am more interested in knowing the best practices to achieve this. </p>
  
  <p>Any idea on this much appreciated?</p>