IncludeMembers in AutoMapper for mapping relations
Ola Johansson
Posted on March 22, 2023
Sometimes you need to grab a value from a relations table, as in this case the sortorder of the specific relation between a "list" and an "item".
Using Automapper i have always had issues with this and usually just been lazy and mapped the other properties manually or used the AfterMap feature.
But today i found out about the IncludeMembers method. It's super simple and you just include the inner property and automapper will treat it just like it's "merged" with the actual source.
CreateMap<ListItemRelation, ItemForCustomList>()
.IncludeMembers(s => s.Item)
.ForMember(dest => dest.SortOrder, o => o.MapFrom(src => src.Sortorder)));
💖 💪 🙅 🚩
Ola Johansson
Posted on March 22, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.