title: Cleanly updating a hierarchy in Entity Framework
body: <p>I'm submitting a form for a <code>StoredProcedureReport</code>, which has many <code>StoredProcedureParameters</code>. Create works fine, but trying to update has left me asking whether or not Microsoft can seriously be serious.</p>
  
  <p>I come from a Rails background where <code>@report.update_attributes(params[:report])</code> will know exactly what to do with whatever association data it finds within. From what I can tell, the .NET equivalent of this is <code>TryUpdateModel</code>, which looked promising. At first. So I tried it with some params like this</p>
  
  <pre><code>IDGUID:d70008a5-a1a3-03d2-7baa-e39c5044ad41
  StoredProcedureName:GetUsers
  Name:search again UPDATED
  StoredProcedureReportParameters[0].IDGUID:d70008a5-aba3-7560-a6ef-30a5524fac72
  StoredProcedureReportParameters[0].StoredProcedureReportID:d70008a5-a1a3-03d2-7baa-e39c5044ad41
  StoredProcedureReportParameters[0].Name:RowsPerPage
  StoredProcedureReportParameters[0].Label:rows
  StoredProcedureReportParameters[0].StoredProcedureReportParameterDataTypeID:a50008a5-2755-54c0-b052-865abf459f7f
  StoredProcedureReportParameters[0].StoredProcedureReportParameterInputTypeID:a50008a5-2955-a593-d00f-00cd4543babf
  StoredProcedureReportParameters[0].DefaultValue:10
  StoredProcedureReportParameters[0].AllowMultiple:false
  StoredProcedureReportParameters[0].Hidden:false
  StoredProcedureReportParameters[1].IDGUID:d70008a5-a7a3-e35e-28b6-36dd9e448ee5
  StoredProcedureReportParameters[1].StoredProcedureReportID:d70008a5-a1a3-03d2-7baa-e39c5044ad41
  StoredProcedureReportParameters[1].Name:PageNumber
  StoredProcedureReportParameters[1].Label:page was MODIFIEIIEIEIED!!!
  StoredProcedureReportParameters[1].StoredProcedureReportParameterDataTypeID:a50008a5-2755-54c0-b052-865abf459f7f
  StoredProcedureReportParameters[1].StoredProcedureReportParameterInputTypeID:a50008a5-2955-a593-d00f-00cd4543babf
  StoredProcedureReportParameters[1].DefaultValue:1
  StoredProcedureReportParameters[1].AllowMultiple:false
  StoredProcedureReportParameters[1].Hidden:false
  </code></pre>
  
  <p>I assumed that with all the primary and foreign keys set, EF would know how to update the <code>StoredProcedureReportParameter</code> objects when I do this:</p>
  
  <pre><code>var report = context.StoredProcedureReports.FirstOrDefault(r =&gt; r.IDGUID == reportID);
  
  if (report != null)
  {
      succeeded = TryUpdateModel(report);
      context.SaveChanges();
  }
  </code></pre>
  
  <p>Now, if I place a breakpoint on <code>context.SaveChanges()</code>, my <code>report</code> object and its associated <code>StoredProcedureReportParameters</code> look just like I'd expect them to. The foreign and primary keys are set, ALL the values check out. But <code>SaveChanges</code> raises this error:</p>
  
  <blockquote>
    <p>The operation failed: The relationship could not be changed because one or more of the foreign-key properties is non-nullable. When a change is made to a relationship, the related foreign-key property is set to a null value. If the foreign-key does not support null values, a new relationship must be defined, the foreign-key property must be assigned another non-null value, or the unrelated object must be deleted.</p>
  </blockquote>
  
  <p>One of the suggestions in this message is that I should assign the foreign-key property a non-null value, but as I stated, <code>StoredProcedureReportID</code> <em>has</em> the correct value on both <code>StoredProcedureReportParameter</code> objects.</p>
  
  <p>Other posts I've read that deal with <code>Update</code> operations loop over associations and attach them to the context. Is this really what I'm stuck doing? Is EF really that dense? I'm hoping for a .NET pro to show me the light here. There <em>has</em> to be an easier way than that.</p>