I have the following configuration.
public class AutoMapperProfile: Profile
{
public AutoMapperProfile()
{
CreateMap<DTO, Model>();
CreateMap<InnerDTO, NavigationPropertyModel>();
}
}
In my code I have
Model.NavigationProperty = mapper.Map(DTO.InnerDTO, Model.NavigationProperty);
seems to work very well but
Model = mapper.Map(DTO, Model);
doesn't. (InnerDTO isn't mapped)
PS: mapper is an instance of the automapper.
I want to stick with the second approach since the DTO can have more properties than just the InnerDTO.
I tried using Mapper.AssertConfigurationIsValid(); but got an exception
System.InvalidOperationException: 'Mapper not initialized. Call Initialize with appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.'
automapper, as long as you define the mappings for all properties you have, which could be properties of the others,automapperknows how to map them correctly. You don't need to manually callModel.NavigationProperty = mapper.Map(). - David LiangModelwithNavigationProperty. Wait for op to check whether my answer will work. - Edward