延迟加载在POST操作后附加

延迟加载在POST操作后附加

本文介绍了EF4.3 code-首先,MVC,延迟加载在POST操作后附加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC 3应用程序中使用实体框架4.3 code-第一。我有一个POST操作,获取一个实体作为其参数,然后根据修改来更新数据库标志着实体。它是有一个文件类型的引用文档实体。

  [HttpPost]
 公众的ActionResult实例(文档模型)
 {
     // filetype的值是零,如预期
     VAR的fileType = model.FileType;     //重视并标记实体修改,保存更改
     Context.Entry(模型).STATE = EntityState.Modified;
     Context.SaveChanges();     // filetype的还是空?
     的fileType = model.FileType;     返回查看(模型);
 }

一个实体连接到上下文后,我不应该能够在该实体延迟加载的属性?

有趣的是,当我尝试这在一个控制台应用程序似乎工作。

 静态无效的主要()
{
    //创建一个新的上下文
    VAR语境=新的上下文();    //获取第一个文档并将其分离
    变种文档= context.Documents.First();
    context.Entry(DOC).STATE = EntityState.Detached;    // filetype的值是零,如预期
    VAR的fileType = doc.FileType;    //处理,并创建一个新的上下文
    context.Dispose();
    上下文=新的上下文();    //附加实体,将其标记为已修改
    context.Entry(DOC).STATE = EntityState.Modified;    // filetype的值是不为空,这是理想的结果
    的fileType = doc.FileType;
}


解决方案

的问题是,传递给POST方法的实体不是一个代理,presumably,因为它是使用正常的实体Framewortk之外创建新运算符。

有这里几个选项。首先,你可以修改MVC控制器使用 DbSet.Create 方法来创建代理实例。我听说有可能修改MVC控制器以这种方式,但从来没有尝试过自己。例如,而不是这样做的:

  VAR DOC =新的文件();

在控制器中,你会怎么做:

  VAR DOC = context.Documents.Create();

Create方法可以让EF创建延迟加载代理,如果实体有相应的虚拟特性,它在你的情况下,它看起来像它。

一个第二和潜在的更容易的选择是不使用延迟加载,而是使用显式加载API。例如,要载入文件类型:

  VAR的fileType = context.Entry(DOC).Reference点(D => d.FileType).Load();

不太可能延迟加载,这需要一个明确的参考范围内,但在你的情况下应该没问题。

I'm using Entity Framework 4.3 with Code-First in an MVC 3 application. I have a POST action that gets an entity as its' parameter, and then marks the entity as modified to update the database. It's a Document entity that has a reference to a 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);
 }

After attaching an entity to the context, shouldn't I be able to lazy-load properties on that entity?

Interestingly, when I try this in a console application it seems to work.

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;
}
解决方案

The problem is that the entity passed to the post method is not a proxy, presumably because it was created outside of the Entity Framewortk using the normal "new" operator.

There are a few options here. First, you could modify the MVC controller to create proxy instances by using the DbSet.Create method. I've heard it is possible to modify the MVC controller in this way, but never tried it myself. For example, instead of doing:

var doc = new Document();

in the controller, you would do:

var doc = context.Documents.Create();

The create method lets EF create a proxy for lazy loading, if the entity has appropriate virtual properties, which in your case it looks like it does.

A second and potentially easier option is to not use lazy loading but instead use the explicit loading APIs. For example, to load FileType:

var fileType = context.Entry(doc).Reference(d => d.FileType).Load();

Unlikely lazy loading, this needs an explicit reference to the context, but in your case that should be okay.

这篇关于EF4.3 code-首先,MVC,延迟加载在POST操作后附加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 14:28