Code sample for my question:
IList<TestDataAnimal> testDataFromDb = this.db.TestDataAnimals.Include(t => t.TestType).Include(t => t.Visit).Include(t => t.Visit.Patient).ToList();
Mapper.CreateMap<TestDataAnimal, TestDataAnimalViewModel>();
IList<TestDataAnimalViewModel> viewModelList = Mapper.Map<IList<TestDataAnimal>, IList<TestDataAnimalViewModel>>(testDataFromDb);
In the above code, testDataFromDb returns all the data that I need. So far, so good… I know I’m getting all the right data from the database. The problem is viewModelList is not populated with data from Visit.Patient.
My viewmodel has properties for FirstName, LastName, MRN, DOB, and PatientId. With the exception of PatientId, which is named Id in Visit.Patient, the property names match in the domain objects and viewmodel.
How do I get AutoMapper to populate my viewmodel properties with the values found in Visit.Patient ? Thanks!
SOLUTION – Added here because I can’t answer my own question…
It’s called flattening and I just found a video here
Basically, this is what I want:
Mapper.CreateMap<TestDataAnimal, TestDataAnimalViewModel>()
.ForMember(d => d.FirstName, opt => opt.MapFrom(s => s.Visit.Patient.FirstName));
It’s called flattening and I just found a video here
Basically, this is what Iwant:
* Found the answer to my own question shortly after posting here but SO said I was unable to answer my own question. Now, I can’t accept my own answer for a few hours.