Must allow top level member
I tried the following card:
CreateMap<ThemeNewModel, CreateThemeOrder.ThemeModel>() .ForMember(d => d.Subject.Id, o => o.MapFrom(s => s.Subject));
Both Subject.Id
and Subject
have a type int
. However, I am getting the following error:
The expression 'd => Transformation (d.Subject.Id)' must resolve on the top-level element and not on properties of child objects. Instead, use a custom mapper for the child type or AfterMap parameter. Parameter name: lambdaExpression
I am using AutoMapper 2.0. Can't I work it out without AfterMap
?
source to share
What is the type ThemeNewModel.Subject
? Assuming it ThemeSubject
, you might have success with something like:
CreateMap<ThemeSubject, CreateThemeOrder.ThemeModel>()
.ForMember(d=>d.Id, o => o.MapFrom(s->s.Subject));
CreateMap<ThemeNewModel, CreateThemeOrder.ThemeModel>()
.ForMember(d=>d.Subject, o => o.MapFrom(s => s);
If the above doesn't work, you should follow the guidelines in the exception and create a custom converter.
Either way, the automapper is designed to flatten from complex types to flat types / model types, so yours is ThemeNewModel
too complex and you might need to rethink your design.
source to share