[英]How to map nested child object properties in Automapper
I have current map: 我有当前的地图:
CreateMap<Article, ArticleModel>()
.ForMember(dest => dest.BaseContentItem, opts => opts.MapFrom(src => src.BaseContentItem))
.ForMember(dest => dest.BaseContentItem.TopicTag, opts => opts.MapFrom(src => src.BaseContentItem.TopicTag))
.ForMember(dest => dest.MainImage, opts => opts.MapFrom(src => src.MainImage))
.ReverseMap();
The error I get is: 我得到的错误是:
System.ArgumentException: 'Expression 'dest => dest.BaseContentItem.TopicTag' must resolve to top-level member and not any child object's properties.
System.ArgumentException:'Expression'dest => dest.BaseContentItem.TopicTag'必须解析为顶级成员而不是任何子对象的属性。 Use a custom resolver on the child type or the AfterMap option instead.'
请改用子类型或AfterMap选项上的自定义解析器。
How can I map this? 我该如何映射?
This should work. 这应该工作。 Use
ForPath
instead of ForMember
使用
ForPath
而不是ForMember
CreateMap<Article, ArticleModel>()
.ForMember(dest => dest.BaseContentItem, opts => opts.MapFrom(src => src.BaseContentItem))
.ForPath(dest => dest.BaseContentItem.TopicTag, opts => opts.MapFrom(src => src.BaseContentItem.TopicTag))
.ForMember(dest => dest.MainImage, opts => opts.MapFrom(src => src.MainImage))
.ReverseMap();
If anyone has an issue, I had strange. 如果有人有问题,我很奇怪。
I created maps for all children correctly in separate mapping profiles. 我在不同的映射配置文件中为所有孩子正确创建了地图 The issue was that one of those child models had a type of TopicTag itself, so it made stack overflow.
问题是其中一个子模型本身就有一种TopicTag,所以它使堆栈溢出。 I removed that unnecessary field and it maps correctly now.
我删除了那个不必要的字段,现在它正确映射。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.