我正在使用实体框架。我有一个“应用程序”对象(它有一个字段 certificateReasonID),它可以有一个或零个 CertificateReasons - 所以“CertificateReason”表有一个关系,并且在 edmx 图上我们看不到 certificateReasonTypeID 字段。
当我更新应用程序时 - 它会插入一个新的 CertificateReason 并将 Application.certificateReasonTypeID 更新到新的 ID - 而不是将 Application.certificateReasonTypeID 更新到所选的 ID。
CertificateReason 对象处于添加状态(技术上正确)。aspx 代码是
<%foreach (var certReason in Model.CertificateReasons)
{ %>
<li>
<%= Html.RadioButton("CertificateReason.id", certReason.id)%>
<!-- only because it is adding when it shouldn't -we have to set the other non null values(i.e. not id) in the object else it will fail when it tries to save-->
<input type="hidden" value="<%= certReason.meaning %>" name="CertificateReason.meaning"/>
<input type="hidden" value="<%= certReason.effectiveFrom %>" name="CertificateReason.effectiveFrom"/>
<input type="hidden" value="<%= certReason.createdWhen %>" name="CertificateReason.createdWhen"/>
<label for="certificateReasonTypeID<%=certReason.meaning%>"><%=certReason.meaning%></label>
</li>
<%}%>
更新代码是
public ActionResult Edit(int id, FormCollection collection, ApplicationViewModel model)
{
var appToUpdate = OMF.Application.First(m => m.id == id);
UpdateModel(movieToUpdate, collection.ToValueProvider());
if (ModelState.IsValid)
{
OMF.ApplyPropertyChanges(movieToUpdate.EntityKey.EntitySetName, appToUpdate );
OMF.SaveChanges();
}
}
谢谢