title: Entity Framework not saving modified children
body: <p>Frustrating, this. Here's a pair of related objects, as generated by database-first Entity Framework:</p>
  
  <pre><code>public partial class DevelopmentType
  {
      public DevelopmentType()
      {
          this.DefaultCharges = new HashSet&lt;DefaultCharge&gt;();
      }
  
      public System.Guid RowId { get; set; }
      public string Type { get; set; }
  
      public virtual ICollection&lt;DefaultCharge&gt; DefaultCharges { get; set; }
  }
  
  public partial class DefaultCharge
  {
      public System.Guid RowId { get; set; }
      public decimal ChargeableRate { get; set; }
      public Nullable&lt;System.Guid&gt; DevelopmentType_RowId { get; set; }
  
      public virtual DevelopmentType DevelopmentType { get; set; }
  }
  </code></pre>
  
  <p>Here's the code that I'm calling to save a DevelopmentType - it involves automapper since we differentiate entity objects from DTOs:</p>
  
  <pre><code>    public void SaveDevelopmentType(DevelopmentType_dto dt)
      {
          Entities.DevelopmentType mappedDevType = Mapper.Map&lt;DevelopmentType_dto, Entities.DevelopmentType&gt;(dt);
          _Context.Entry(mappedDevType).State = System.Data.EntityState.Modified;
  
          _Context.DevelopmentTypes.Attach(mappedDevType);
          _Context.SaveChanges();
      }
  </code></pre>
  
  <p>In my user interface, the most common operation will be for a user to look at a list of DevelopmentTypes and update their DefaultCharge. So when I test this using the above code, it runs without error, but nothing actually changes.</p>
  
  <p>If I pause in the debugger it's clear that the changed DefaultCharge is being passed into the function, and that it's attached to the DevelopmentType to be saved. </p>
  
  <p>Stepping through it, if I change the value manually inside visual studio, it <em>does</em> save the updated value. Which is just even more confusing.</p>
  
  <p>Monitoring the database with SQL Server Profiler reveals that update commands are issued <em>only</em> for the parent object and <em>not</em> for any attached objects.</p>
  
  <p>I have other similar code elsewhere that functions as expected. What am I doing wrong here?</p>
  
  <p>EDIT:</p>
  
  <p>I have discovered that if you do this prior to the call to SaveDevelopmentType:</p>
  
  <pre><code>        using (TransactionScope scope = new TransactionScope())
          {
              dt.Type = "Test1";
              dt.DefaultCharges.First().ChargeableRate = 99;
              _CILRepository.SaveDevelopmentType(dt);
              scope.Complete();
          }
  </code></pre>
  
  <p>The change to Type saves, but the change to ChargeableRate does not. I don't think it helps, massively, but thought I'd add it.</p>