asp.net-mvc 使用 Automapper 更新现有实体 POCO

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13314666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 02:24:16  来源:igfitidea点击:

Using Automapper to update an existing Entity POCO

asp.net-mvcentity-framework-4viewmodelautomapperdbcontext

提问by Paul Taylor

I am using EF4 DbContext to provide the model for an ASP.NET MVC app. I use ViewModels to provide data to the views and Automapper to perform the mapping between the EF POCOs and the ViewModels. Automapper does a great job but I'm not clear the best way to use it after the ViewModel is posted back to the controller to carry out an update.

我正在使用 EF4 DbContext 为 ASP.NET MVC 应用程序提供模型。我使用 ViewModel 向视图提供数据,并使用 Automapper 执行 EF POCO 和 ViewModel 之间的映射。Automapper 做得很好,但我不清楚在将 ViewModel 回发到控制器以执行更新后使用它的最佳方法。

My idea is to get POCO object using a key contained in the ViewModel. I then want to use Automapper to update the POCO with data from the ViewModel:

我的想法是使用 ViewModel 中包含的键来获取 POCO 对象。然后我想使用 Automapper 用 ViewModel 中的数据更新 POCO:

[HttpPost]
public ActionResult Edit(PatientView viewModel)
{
    Patient patient = db.Patients.Find(viewModel.Id); 
    patient = Mapper.Map<ViewModel, Patient>(viewModel, patient);
    ...
    db.SaveChanges();
    return RedirectToAction("Index");
}

Two questions:

两个问题:

  1. The Find() method returns a Proxy rather than a POCO which causes Automapper to complain. How do I get the POCO instead of the Proxy?
  2. Is this best practice for performing an update?
  1. Find() 方法返回一个 Proxy 而不是 POCO,这会导致 Automapper 抱怨。我如何获得 POCO 而不是代理?
  2. 这是执行更新的最佳实践吗?

回答by Marius St?nescu

If you use Automapper like that, it returns a new Patient object and the references to the enity framework graph are not kept. You have to use it like this:

如果您像这样使用 Automapper,它会返回一个新的 Patient 对象,并且不会保留对实体框架图的引用。你必须像这样使用它:

[HttpPost]
public ActionResult Edit(PatientView viewModel)
{
    Patient patient = db.Patients.Find(viewModel.Id); 
    Mapper.Map(viewModel, patient);
    ...
    db.SaveChanges();
    return RedirectToAction("Index");
}

回答by Paul Taylor

There seem to be two approaches to dealing with the EF proxy issue:

处理 EF 代理问题似乎有两种方法:

  1. Switch off ObjectContext.ContextOptions.ProxyCreationEnabled, either for the whole application (in EF Context constructor or EDMX), or for the query where you need to guarantee getting an actual Entity object rather than a proxy.
  2. Using an extension to Automapper, documented here: https://gist.github.com/935461.
  1. 关闭ObjectContext.ContextOptions.ProxyCreationEnabled,对于整个应用程序(在 EF 上下文构造函数或 EDMX 中),或者对于需要保证获取实际实体对象而不是代理的查询。
  2. 使用 Automapper 的扩展,记录在此处:https: //gist.github.com/935461

Note. The latter is commented with "Room for improvement. See: Automapper : mapping issue with inheritance and abstract base class on collections with Entity Framework 4 Proxy Pocos".

笔记。后者被评论为“改进空间。请参阅:Automapper:使用 Entity Framework 4 Proxy Pocos 的集合上的继承和抽象基类的映射问题”。