2
我在MVC 3應用程序中使用實體框架4.3和Code-First。我有一個POST操作獲取實體作爲其參數,然後將實體標記爲已修改以更新數據庫。它是一個文檔實體,它具有對文件類型的引用。EF4.3代碼優先,MVC,加入POST後的延遲加載操作
[HttpPost]
public ActionResult Example(Document model)
{
// fileType is null, as expected
var fileType = model.FileType;
// attach and mark the entity as modified, save changes
Context.Entry(model).State = EntityState.Modified;
Context.SaveChanges();
// fileType is still null?
fileType = model.FileType;
return View(model);
}
將一個實體附加到上下文之後,我不應該能夠在該實體上延遲加載屬性嗎?
有趣的是,當我在控制檯應用程序中嘗試這個時,它似乎工作。
static void Main()
{
// create a new context
var context = new Context();
// get the first document and detach it
var doc = context.Documents.First();
context.Entry(doc).State = EntityState.Detached;
// fileType is null, as expected
var fileType = doc.FileType;
// dispose and create a new context
context.Dispose();
context = new Context();
// attach the entity and mark it as modified
context.Entry(doc).State = EntityState.Modified;
// fileType is not null, which is the desired outcome
fileType = doc.FileType;
}
謝謝!使用你的解釋,我能夠找到一個簡單的解決方案來實現一個生成代理的模型聯編程序。這裏是幫助我的文章的鏈接。 http://bit.ly/ydCjOm – user326502 2012-03-14 04:30:40
@ user326502感謝您的鏈接。超級有用 – 2015-03-19 09:33:09