【发布时间】:2012-03-14 11:38:49
【问题描述】:
我在 MVC 3 应用程序中使用 Entity Framework 4.3 和 Code-First。我有一个 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;
}
【问题讨论】:
标签: asp.net-mvc entity-framework lazy-loading model-binding