title: Storing a complex detached object graph in EF6
body: <p>I have the current scenario:</p>
  
  <p>I'm using EF6 Code first, and have created a data-model something like this:</p>
  
  <pre><code>public class MainObject{
    ..some properties
    IList&lt;SubObject&gt; SubObjects{get;set;}
  }
  
  public class SubObject{
    ..some properties
    IList&lt;SubSubObject&gt; SubSubObjects{get;set;}
  }
  
  public class SubObject{
    ..some properties
    IList&lt;SubObject&gt; SubObjects{get;set;}
  }
  </code></pre>
  
  <p>So basically I have a main object, that has 0 to many subobjects, and there is a many to many relationship between the subobject and the subsubobjects.</p>
  
  <p>I am creating a MVC application, so my normal program flow is that the user request a page that basically uses a MainObject as it's data model. Then the user interacts with the page and changes, adds or removes subobjects and subsubobjects as he wishes, and then clicks save. On save, the objectgraph is sent back to the controller and it looks correct according to the changes done by the user on the client side. Now my problem is the following:</p>
  
  <p>How to store this back into the database in a good fashion.</p>
  
  <p>I need to attach my object back into the context, but I have no clue which objects are new, modified or deleted.</p>
  
  <p>I have written some code that partially works now, but it's getting so ugly that I really don't want to go down that path. Would it be possible somehow to fetch the relevant object graph from the database, and have EF compare the two graphs toward eachother, and then save the relevant changes to the database?</p>
  
  <p>Any help to make this smoother would be greatly appreciated.</p>