我想要做的事情非常簡單。我有兩個類:實體框架4.1 RC(代碼優先) - 實體沒有通過關聯更新
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類的記錄類型的財產,我發現,該協會無法更新。不會拋出異常,但不執行更新:
[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),因爲有一些驗證,但它有效。對不起,我錯過了您的其他答案,但非常感謝您的指導! – 2011-04-01 12:26:13
[Entity Framework Code First - 爲什麼我不能以這種方式更新複雜屬性?](http://stackoverflow.com/questions/5506116/entity-framework-code-first-why-cant-i-update -complex-properties-this-way) – MikroDel 2013-07-31 12:25:24