title: Entity Framework 6 Update Graph
body: <p>What is the correct way to save a graph of objects whose state you don't know? By state I mean whether they are new or existing database entries that are being updated.</p>
  
  <p>For instance, if I have:</p>
  
  <pre><code>public class Person
  {
       public int Id { get; set; }
       public int Name { get; set; }
       public virtual ICollection&lt;Automobile&gt; Automobiles { get; set; }
  
  }
  
  public class Automobile
  {
       public int Id { get; set; }
       public int Name { get; set; }
       public short Seats { get; set; }
       public virtual ICollection&lt;MaintenanceRecord&gt; MaintenanceRecords { get; set ;}
       public virtual Person Person { get; set; }
  }
  
  public class MaintenanceRecord
  {
       public int Id { get; set; }
       public int AutomobileId { get; set; }
       public DateTime DatePerformed { get; set; }
  
       public virtual Automobile Automobile{ get; set; }
  
  }
  </code></pre>
  
  <p>I'm editing models, similar to these objects above, and then passing those models into the data layer to save, where for this instance I happen to be using entity framework. So I'm translating these models into POCO entities internal to the DAL.</p>
  
  <p>It appears that unless my models have a state indicating whether they are new or updated, I have quite a bit of work to do to "Save" the changes. I have to first select the Person entity, update it, then match any existing Automobiles and update those and add any new, then for each automobile check for any new or updated maintenance records. </p>
  
  <p>Is there a faster/easier way of doing this? It's possible I can keep track of the Model state, which I guess would be helpful with this, but it would mean changes to code outside of the data layer which i would prefer to avoid. I'm just hoping there is a pattern of usage out there that I can follow for updates like this.</p>