I'm submitting a form for a StoredProcedureReport
, which has many StoredProcedureParameters
. Create works fine, but trying to update has left me asking whether or not Microsoft can seriously be serious.
I come from a Rails background where @report.update_attributes(params[:report])
will know exactly what to do with whatever association data it finds within. From what I can tell, the .NET equivalent of this is TryUpdateModel
, which looked promising. At first. So I tried it with some params like this
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
I assumed that with all the primary and foreign keys set, EF would know how to update the StoredProcedureReportParameter
objects when I do this:
var report = context.StoredProcedureReports.FirstOrDefault(r => r.IDGUID == reportID);
if (report != null)
{
succeeded = TryUpdateModel(report);
context.SaveChanges();
}
Now, if I place a breakpoint on context.SaveChanges()
, my report
object and its associated StoredProcedureReportParameters
look just like I'd expect them to. The foreign and primary keys are set, ALL the values check out. But SaveChanges
raises this error:
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.
One of the suggestions in this message is that I should assign the foreign-key property a non-null value, but as I stated, StoredProcedureReportID
has the correct value on both StoredProcedureReportParameter
objects.
Other posts I've read that deal with Update
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 has to be an easier way than that.