Using Automapper to update an existing Entity POCO
Asked Answered
C

2

26

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.

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:

[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?
Corriecorriedale answered 9/11, 2012 at 19:29 Comment(3)
This might help: #6157038Stella
I think this is OK, elegant even. You use the correct overload of Mapper.Map.Mastoidectomy
Thanks @Gert Arnold, increased my confidence in that approach.Corriecorriedale
S
74

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:

[HttpPost]
public ActionResult Edit(PatientView viewModel)
{
    Patient patient = db.Patients.Find(viewModel.Id); 
    Mapper.Map(viewModel, patient);
    ...
    db.SaveChanges();
    return RedirectToAction("Index");
}
Sewage answered 11/8, 2014 at 11:34 Comment(1)
Yes, I believe an bug has been fixed since I posted this question, and the method you illustrate now works correctly.Corriecorriedale
C
1

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

  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.

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".

Corriecorriedale answered 12/11, 2012 at 9:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.