我想做的很简单。我有两节课:
public class TownRecord
{
public int Id { get; set; }
public string ShortName { get; set; }
public string FileName { get; set; }
public string tags { get; set; }
public virtual TownRecordType RecordType { get; set; }
public DateTime? DateScanned { get; set; }
public DateTime? RecordDate { get; set; }
[StringLength(4000)]
public string Comments { get; set; }
public string UploadedBy { get; set; }
}
public class TownRecordType
{
public int Id { get; set; }
public string RecordType { get; set; }
public virtual ICollection<TownRecord> TownRecords {get; set; }
}
当我想更新 TownRecord 类的 RecordType 属性时,我发现关联更新失败。不抛出异常但不执行更新:
[HttpPost]
public ActionResult Edit(int id, TownRecord tr, FormCollection collection)
{
TownRecordType newRecType = _ctx.TownRecordTypes.Find(Int32.Parse(collection["RecordType"]));
tr.RecordType = newRecType;
_ctx.Entry(tr).State = EntityState.Modified;
_ctx.SaveChanges();
return RedirectToAction("List");
}
注意:为了清楚起见,我删除了我的错误处理......
我在这里看到了与此类似的问题,但我不明白。这可能是一个非常愚蠢的新手错误,但我已经 StackOverflowing 和谷歌搜索了几个小时,却一无所获。任何帮助是极大的赞赏。