title: Updating object with child collection using Entity Framework causing duplicates in database
body: <p>I have a <code>Customer</code> class that has a relationship to an <code>Address</code> class:</p>
  
  <pre><code>public class Customer
  {
      public int Id { get; set; }
  
      public string Name { get; set; }
  
      public virtual ICollection&lt;Address&gt; Addresses { get; set; }
  }
  
  public class Address
  {
      public int Id { get; set; }
  
      public string Street1 { get; set; }
  
      //Snip a bunch of properties
  
      public virtual Customer Customer { get; set; }
  }
  </code></pre>
  
  <p>I have an edit form which displays all the fields for both the customer and address. When this form is submitted, it calls the <code>Edit</code> method in the controller:</p>
  
  <pre><code>public ActionResult Save(Customer customer)
  {
      if (!ModelState.IsValid)
      {
          var viewModel = new CustomerFormViewModel
          {
              Customer = customer,
              CustomerTypes = _context.CustomerTypes.ToList()
          };
  
          return View("CustomerForm", viewModel);
       }
  
       if (customer.Id == 0)
           _context.Customers.Add(customer);
       else
       {
           var existingCustomer = _context.Customers
               .Include(c =&gt; c.Addresses)
               .Single(c =&gt; c.Id == customer.Id);
  
           existingCustomer.Name = customer.Name;
           existingCustomer.TaxId = customer.TaxId;
           existingCustomer.CustomerTypeId = customer.CustomerTypeId;
           existingCustomer.CreditLimit = customer.CreditLimit;
           existingCustomer.Exempt = customer.Exempt;
           existingCustomer.Addresses = customer.Addresses;
       }
  
       _context.SaveChanges();
  
       return RedirectToAction("Index", "Customers");
  }
  </code></pre>
  
  <p>This doesn't work and creates duplicate entries in the <code>Addresses</code> table in the DB. I think I understand why (EF isn't smart enough to know the Addresses inside the collection need to be added/modified/deleted as the case may be). So, what is the best way to fix this?</p>
  
  <p>My instinct is that I need to iterate over the <code>Addresses</code> collections and compare them manually, adding any new ones from the form that don't exist for the customer, updating ones that do exist, and deleting ones that were not sent by the form but exist in the DB for the customer. Something like (ignoring the delete functionality for now):</p>
  
  <pre><code>foreach(Address address in customer.Addresses)
  {
      if (address.Id == 0)
          // Add record
      else
          // Fetch address record from DB
          // Update data
      }
      // Save context
  </code></pre>
  
  <p>Is this the best way to go about this, or are there any EF tricks to iterating and syncing a child collection to the DB?</p>
  
  <p>Oh, and one question which has me scratching my head - I can sort of understand how a new address record is getting created in the DB, but what I don't get is the existing address record is also updated to have its <code>customer_id</code> set to NULL...how the heck does that happen? That leads me to believe that EF does see the original address record is somehow linked (as it is modifying it) but it's not smart enough to realize the record I'm passing in should replace it?</p>
  
  <p>Thanks -- also, this is EF6 and MVC5</p>