I have a model that is my base model that has numerous fields in it. I then have 4 DTOs that are parts of that model. I want to map all of them to the model in the end but each time I map it overwrites the previous mapping.
For example
_model = new GenericModel();
_model.ExampleNotInDTO = "This Gets Overwritten being set previously";
//First Mapping below overwrites the property I set above and
// sets only the fields in the business dto.
Mapper.CreateMap<BusinessDto, GenericModel>();
_model = Mapper.Map<BusinessDto, GenericModel>(searchResultsQuery.BusinessDto);
//Now doing another mapping just below it nulls out all the previous
// stuff and only fills in the events dto.
Mapper.CreateMap<EventsDto, GenericModel>();
_model = Mapper.Map<EventsDto, GenericModel>(searchResultsQuery.EventsDto);
How would be the best way to get all 4 of my dtos (only 2 above for example sake) into the same _model object?
You’re asking AutoMapper to create a new object each time when you call
Mapthat way. You can create the object manually and useMap<TSource, TDestination>(TSource source, TDestination destination)to do what you want. For example:or