3
votes

I tried the following map:

CreateMap<ThemeNewModel, CreateThemeOrder.ThemeModel>()
    .ForMember(d => d.Subject.Id, o => o.MapFrom(s => s.Subject));

Both Subject.Id and Subject are of type int. However, I get the following error:

Expression 'd => Convert(d.Subject.Id)' must resolve to top-level member and not any child object's properties. Use a custom resolver on the child type or the AfterMap option instead.Parameter name: lambdaExpression

I am using AutoMapper 2.0. Can't I solve this without AfterMap?

1

1 Answers

3
votes

What is the type of ThemeNewModel.Subject? Assuming its ThemeSubject, you may 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 does not work, you should follow the advise in the exception, and create a custom resolver.

Anyway, automapper is designed to flatten from complex types to a flat model/viewmodel types, so your ThemeNewModel is too complex, and maybe you need to rethink your design.