I have four classes:
Application, CaperData, ApplicationDTO, CaperDataDTO
Application is inheritting from CaperData as follows:
public class Application : CaperData
And in ApplicationDTO, we have a property of type CaperDataDTO.
CaperData and CaperDataDTO are identical on the properties level.
Application and ApplicationDTO are also identical with 1 difference that the first one extends from CaperData but the second one does not extend from CaperDataDTO, instead, it has the property CaperDataDTO inside of it.
I need to map all the clases together in autoMapper
Application with ApplicationDTO and vice versa
CaperData with CaperDataDTO and vice versa
using AutoMapper;
public class Application : CaperData
{
// Properties and methods specific to Application
}
public class CaperData
{
// Properties and methods specific to CaperData
}
public class ApplicationDTO
{
public CaperDataDTO CopData { get; set; }
// Other properties and methods specific to ApplicationDTO
}
public class CaperDataDTO
{
// Properties and methods specific to CaperDataDTO
}
How can I do the mapping configuration in MappingProfile?
What I tried so far is:
CreateMap<CaperData, CaperDataDTO>();
CreateMap<CaperDataDTO, CaperData>();
CreateMap<Application, ApplicationDTO>()
.ForMember(dest => dest.CapData, opt => opt.MapFrom(src => src));
CreateMap<ApplicationDTO, Application>()
.ForMember(dest => dest.AppName, opt => opt.MapFrom(src => src.CapData.AppName))
.ForMember(dest => dest.ErrorMessage, opt => opt.MapFrom(src => src.CapData.ErrorMessage))
.ForMember(dest => dest.Reason, opt => opt.MapFrom(src => src.CapData.Reason));
When I try to map, as follows:
List<Application> apps = _appRepo.GetData();
var mappedApps = _mapper.Map<ApplicationDTO>(apps );
I am receiving the following exception:
Missing type map configuration or unsupported mapping.
Mapping types:
List`1 -> ApplicationDTO
System.Collections.Generic.List`1[[DAL.Model.Application, DAL, Version=3.12.0.0, Culture=neutral, PublicKeyToken=null]] -> Client.Models.ApplicationDTO`