'Map simple object to destination with multiple child objects
Given following classes get an error as shown below.
public class CPRequest
{
public ChannelType? ChannelType { get; set; }
public bool IsJoint { get; set; }
public int RiskTier { get; set; }
}
public class CPGR
{
public CPGR();
public AppSumm AppSumm { get; set; }
public LS LS { get; set; }
}
public class AppSumm
{
public AppSumm();
public int RiskTier { get; set; }
public bool IsJoint { get; set; }
}
public class LS
{
public Channel Channel { get; set; }
}
public class Channel
{
public Channel();
public ChannelType ChannelType { get; set; }
public string ChannelCode { get; set; }
public List<string> ChannelGroupCodes { get; set; }
}
public class MapperProfile
{
public MapperProfile()
{
CreateMap<CPRequest, Channel>()
.ForMember(d => d.ChannelType, opt => opt.MapFrom(s => s.ChannelType));
CreateMap<CPRequest, LS>()
.ForMember(d => d.Channel, opt => opt.MapFrom(s => s.ChannelType))
.ForAllOtherMembers(opt => opt.Ignore());
CreateMap<CPRequest, AppSumm>()
.ForMember(d => d.RiskTier, opt => opt.MapFrom(s => s.RiskTier))
.ForMember(d => d.IsJoint, opt => opt.MapFrom(s=>s.IsJoint))
.ForAllOtherMembers(opt => opt.Ignore());
CreateMap<CPRequest, CPGR>()
.ForMember(d => d.AppSumm, opt => opt.MapFrom(s => s))
// .ForMember(d=>d.LS,opt=>opt.MapFrom(s=>s))
.ForAllOtherMembers(opt => opt.Ignore())
;
}
}
Usage: var req = _mapper.Map(request); Error: Error mapping types. Mapping types:
CPRequest -> CPGR CPRequest -> CPGR
Type Map configuration: CPRequest -> CPGR CPRequest -> CPGR Destination Member: LS
How to use C# and Automapper to map properties from CPRequest onto CPGR
- Map IsJoint to AppSumm.IsJoint
- Map RiskTier to AppSumm.RiskTier
- Map ChannelType to LS.LS
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
