Scenario:
A huge Patient
class/entity with around 100 properties. But in the webpage I only need to display the contact information (only around 10 properties) to update. Do/Can I create a ViewModel to do that?
I have only used ViewModel to read data before. Can it be used to update data?
EDIT: Okay, to summarize what I have found so far.
- Use
AutoMapper
to map the ViewModel (only contains the properties to be updated) back to the original big EntityClass. An example is:
[HttpPost]
public virtual ActionResult Edit(EditUser user)
{
var domain = uow.Users.Create();
domain.Id = user.Id;
uow.Users.Update(domain);
AutoMapper.Mapper.Map<EditUser, Example.Core.Data.User>(user, domain);
uow.Save();
return View();
}
- Create another entity called PatientSummary in the EDMX model, which only contain the necessary properties. Manually do the mapping in the EDMX designer. Then use this reduced entity for the Patient contact update web page.
Personally, I feel there must be a better way to do that. Any comment?