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 POCO
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");
}
There seem to be two approaches to dealing with the EF proxy issue:
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.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".