【发布时间】:2011-03-31 23:26:41
【问题描述】:
我想要做的很简单。我有两个班级:
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");
}
注意:为了清楚起见,我删除了我的错误处理...
我看到一个与here 类似的问题,但我不明白。这可能是一个非常愚蠢的新手错误,但我已经 StackOverflowing 和谷歌搜索了几个小时,却一无所获。非常感谢任何帮助。
【问题讨论】:
-
拉迪斯拉夫,谢谢。您的解决方案有效。由于一些验证,我仍然需要设置 RecordType 属性(tr.RecordType = new RecType),但它可以工作。抱歉,我错过了您的其他答案,但非常感谢您的指导!
-
我投票结束这个问题,因为它是关于一个候选版本,也是一个过时的 EF 版本。
标签: c# entity-framework code-first entity-framework-4.1