title: Entity Framework - Updating entity with added/modified child and grandchild entities
body: <p>I am not sure if I am doing this the right way or not, so need advice.</p>
  
  <p>I have an entity, this entity has a child collection, and each child entity has another child collection. Something like this (simplified example)</p>
  
  <pre><code>public MyEntity() {
      public long Id { get; set; }
      public ICollection&lt;MyChild&gt; Children { get; set; }
  }
  
  public MyChild() {
      public long Id { get; set; }
      public long MyEntityId { get; set; }
      public MyEntity MyEntity { get; set; }
      public ICollection&lt;MyGrandChild&gt; Children { get; set; }
  }
  
  public MyGrandChild() {
      public long Id { get; set; }
      public long MyChildId { get; set; }
      public MyChild MyChild { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>Now in our application, the user retrieves this entity from our webApi into an angularJs application.  The user then updates the entity (and sub entities) and passes the entity back to the webApi.  I am using models to pass the objects from my webApi to the angularJs application, and they look something like this.</p>
  
  <pre><code>public MyEntityModel() {
      public long Id { get; set; }
      public ICollection&lt;MyChildModel&gt; Children { get; set; }
  }
  
  public MyChildModel() {
      public long Id { get; set; }
      public ICollection&lt;MyGrandChildModel&gt; Children { get; set; }
  }
  
  public MyGrandChildModel() {
      public long Id { get; set; }
      public string Name { get; set; }
  }
  </code></pre>
  
  <p>Once the models are passed back to the webApi, I use Auto Mapper to convert them back to entity objects.</p>
  
  <p>Now the bit I am confused about, i now pass the object to my service layer, my method looks similar to this</p>
  
  <pre><code>public Task&lt;int&gt; UpdateAsync(MyEntity updated) {
      _context.Entry(updated).State = EntityState.Modified;
  
      return _context.SaveChangesAsync();
  }
  </code></pre>
  
  <p>If I add a new <code>MyChild</code> or <code>MyGrandChild</code> object to <code>MyEntity</code> after MyEntity exists or update <code>MyChild</code> or <code>MyGrandChild</code> object then the changes are not committed to the database?  I changed my <code>UpdateAsync</code> method to this, but is this really needed?</p>
  
  <pre><code>public Task&lt;int&gt; UpdateAsync(MyEntity updated) {
      _context.Entry(updated).State = EntityState.Modified;
  
      foreach (var child in updated.Children) {
          if (child.Id == 0) {
              _context.Entry(child).State = EntityState.Added;
          } else {
              _context.Entry(child).State = EntityState.Modified;
          }
  
          foreach (var grand in child.Children) {
              if (grand.Id == 0) {
                  _context.Entry(grand).State = EntityState.Added;
              } else {
                  _context.Entry(grand).State = EntityState.Modified;
              }
          }
      }
  
      return _context.SaveChangesAsync();
  }
  </code></pre>
  
  <p>Do I really have to loop through each collection, and sub collection, check if the id equals 0 and set its state accordingly?  </p>