我在MVC 3应用程序中使用Entity Framework 4.3和Code-First.我有一个POST操作,它将实体作为其'参数,然后将实体标记为已修改以更新数据库.它是一个Document实体,它引用了File Type.
[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);
}
Run Code Online (Sandbox Code Playgroud)
将实体附加到上下文后,我不应该在该实体上延迟加载属性吗?
有趣的是,当我在控制台应用程序中尝试这个时,它似乎工作.
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 …Run Code Online (Sandbox Code Playgroud)