0

So the error is this: Expression must resolve to top-level member and not any child object's properties.

Remuneration in the DTO is a Enum. ContractEntity uses a Remuneration that is a ComplexType.

The code throwing the error:

Mapper.CreateMap<ContractDTO, ContractEntity>() .ForMember(d => d.Remuneration.ContractType, s => s.MapFrom(z => z.ContractType)) .ForMember(d => d.Remuneration.Currency, s => s.MapFrom(z => z.Currency)) .ForMember(d => d.Remuneration.RateUnit, s => s.MapFrom(z => z.RateUnit)); 

Entity Framework complex type:

 [ComplexType] public class Remuneration { public decimal Amount { get; set; } public int Currency { get; set; } public int RateUnit { get; set; } public int ContractType { get; set; } } 

Because I want the destination (ContractEntity) to use the integer values, I thought I could just cast the source enum to the destination integer like this:

.ForMember(d => d.Remuneration.ContractType, s => s.MapFrom(z => (int)z.ContractType)) 

.. obviously I cant, and was hoping that someone could clarify why this doesn't work..

1 Answer 1

1

If you have problmes with complex type mapping, you can implement manual mapping based on AutoMapper:

Mapper.CreateMap<PatternDto, PatternModel>().ConvertUsing(pattern => { if(pattern == null) return new PatternModel(); return new PatternModel { EmailPattern = pattern.EmailPattern, SmsPattern = pattern.SmsPattern }; }); 

Good luck!

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Andrei - tried that but the problem is that ContactEntity has a ComplexType of Remuneration which has properties that I cant access using your example. In other words: PatternModel has a complextype of MessagePatterns which have properties of EmailPattern and SMSPatterns.
And again it is possible somehow to solve your problem with ConvertUsing
Possibly, the question is how?
Tried this as well but it doesnt work: Mapper.CreateMap<ContractType, int>().ConvertUsing(pattern => (int)pattern); Mapper.CreateMap<Currency, int>().ConvertUsing(pattern => (int)pattern); Mapper.CreateMap<RateUnit, int>().ConvertUsing(pattern => (int)pattern);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.