title: Updating related Data with Entity Framework 6 in MVC 5
body: <p>Using EntityFramework 6, I would like to update Customer in the following scenario:</p>
  
  <pre><code>public class Customer
  {
      public int Id {get; set;}
      // 100 more scalar properties
      public virtual IList&lt;Consultant&gt; Consultants {get;set;}
  }
  
  public class Consultant
  {
      public int Id {get; set;}
      public virtual IList&lt;Customer&gt; Customers{get;set;}
  }
  </code></pre>
  
  <p>This is my ViewModel for the edit view:</p>
  
  <pre><code>public class CustomerViewModel
  {
      public string[] SelectedConsultants { get; set; }
      public IEnumerable&lt;Consultants&gt; AllConsultants{ get; set; }
      public Customer Customer{ get; set; }
  }
  </code></pre>
  
  <p>This is my Edit-ActionMethod:</p>
  
  <pre><code>    [HttpPost]
      public ActionResult Edit(CustomerViewModel vm)
      {
          if (ModelState.IsValid)
          {
              // update the scalar properties on the customer
              var updatedCustomer = vm.Customer;
              _db.Customers.Attach(updatedCustomer );
              _db.Entry(updatedCustomer ).State = EntityState.Modified;
              _db.SaveChanges();
  
              // update the navigational property [Consultants] on the customer
              var customer = _db.Customers
                          .Include(i =&gt; i.Customers)
                          .Single(i =&gt; i.Id == vm.Customer.Id);
  
              Customer.Consultants.Clear();
              _db.Consultants.Where(x =&gt; vm.SelectedConsultants
                         .Contains(x.Id)).ToList()
                         .ForEach(x =&gt; customer.Consultants.Add(x));
  
              _db.Entry(customer).State = EntityState.Modified;
              _db.SaveChanges();
              return RedirectToAction("Index");
          }
          return View(vm);
      }
  </code></pre>
  
  <p>This works and both scalar properties and consultants are updateable from the edit view. However, I am doing two <code>_db.SaveChanges();</code> in my controller. Is there a less complex way to update <code>Customer</code>? Because <code>Customer</code> has many properties, I'd preferably not do a manual matching of all parameters on <code>Customer</code> and <code>vm.Customer</code>.</p>
  
  <p>I have found the following resources:</p>
  
  <ol>
  <li><a href="http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/updating-related-data-with-the-entity-framework-in-an-asp-net-mvc-application" rel="nofollow noreferrer">asp.net official</a> seems overly complicated (see section <em>Adding
  Course Assignments to the Instructor Edit Page</em>) plus would require
  me to explicitly write all parameters of Customer)</li>
  <li><a href="https://stackoverflow.com/questions/15336248/entity-framework-5-updating-a-record">this popular thread</a> on SO. Method 3 looks like what I need but I could not get the navigational property updated.</li>
  </ol>