title: Entity Framework Service Layer Update POCO
body: <p>I am using the <code>Service Layer --&gt; Repository --&gt; Entity Framework (Code-First) w/POCO objects</code> approach, and I am having a hard time with updating entities.</p>
  
  <p>I am using AutoMapper to map my Domain Objects to my View Models and that works good for getting the data, no how do I get that changes back into the database?</p>
  
  <p>Using pure POCO objects, I would assume that there is no sort of change tracking, so I see my only option is to handle it myself. Do you just make sure that your View Models have the EXACT same properties as your Domain Objects? What if I just change a field or two on the View Model? Won't the rest of the fields on the Domain Object get overwritten in the database with default values?</p>
  
  <p>With that said, what is the best approach?</p>
  
  <p>Thanks!</p>
  
  <p><strong>Edit</strong></p>
  
  <p>So what I am stumbling on is this, lets take for example a simple <code>Customer</code>:</p>
  
  <p>1) The <code>Controller</code> has a service, <code>CustomerService</code>, that calls the services <code>GetCustmoerByID</code> method.</p>
  
  <p>2) The <code>Service</code> calls into the <code>CustomerRepository</code> and retrieves the <code>Customer</code> object.</p>
  
  <p>3) <code>Controller</code> uses AutoMapper to map the <code>Customer</code> to the <code>ViewModel</code>.</p>
  
  <p>4) <code>Controller</code> hands the model to the <code>View</code>. Everything is great!</p>
  
  <p>Now in the view you do some modifications of the customer and post it back to the controller to persist the changes to the database.</p>
  
  <p>I would assume at this point the object is detached. So should the model have the EXACT same properties as the <code>Customer</code> object? And do you have to make hidden fields for each item that you do not want to show, so they can persist back?</p>
  
  <p>How do you handle saving the object back to the database? What happens if your view/model only deals with a couple of the fields on the object?</p>