Reputation: 1581
I am trying to set up an AutoMapper
profile and am stuck trying to map from an entity into this view model:
public class CompositeViewModel
{
public ContactViewModel Contact;
public CompanyViewModel Company;
}
My current profile contains mappings from Contact -> ContactViewModel and Company -> CompanyViewModel which both work perfectly. But what I want to do is map a single Contact
into this composite view model. The Contact
class has an instance of Company
as a property as in: contact.Company
.
When I do:
var viewModel = Mapper.Map<Contact, CompositeViewModel>(contact);
It properly fills in CompositeViewModel.Company
with the details in contact.Company
but I would like all of the entities properties copied into CompositeViewModel.Contact
.
The only solution I can see at the moment is removing the ContactViewModel.Contact
property and flattening it out with all of the Contact
properties that I need. But this seems like it should be unnecesasry.
If I haven't been clear enough let me know, and I will try and elaborate. Thanks.
Upvotes: 0
Views: 116
Reputation: 42669
Simpler would be to do something like
var viewModel=new viewModel
viewModel.Contact=Mapper.Map<Contact,ContactViewModel>(contact)
viewModel.Company=Mapper.Map<Company,CompanyViewModel>(contact.Company)
Read automapper documentation to see how customization of mapping works, in case you are not satisfied with the above approach.
Upvotes: 1