[英]Automapper: Mapping list to object
Im having problems mapping my domain to my DTO.我在将我的域映射到我的 DTO 时遇到问题。
The error is: Expression must resolve to top-level member and not any child object's properties.错误是:表达式必须解析为顶级成员,而不是任何子对象的属性。 You can use ForPath, a custom resolver on the child type or the AfterMap option instead.
您可以使用 ForPath、子类型上的自定义解析器或 AfterMap 选项。
public class Sign
{
public List<Item> Items { get; set; } = new List<Item>();
}
public class SignDTO
{
public SignItemDTO Items { get; set; } = new SignItemDTO();
}
public class SignItemDTO
{
public List<ItemDTO> Items { get; set; } = new List<ItemDTO>();
}
private MapperConfiguration AutoMapperConfig()
{
return new MapperConfiguration(cfg =>
{
cfg.CreateMap<Item, ItemDTO>();
cfg.CreateMap<Sign, SignDTO>().ForPath(dest => dest.Items.Items, opt => opt.MapFrom(src => src.Items));
});
}
_context.Sign.Include(m => m.Items)
.ProjectTo<SignDTO>(AutoMapperConfig());
Probably it's due to your map in this line:可能是由于您在此行中的 map :
cfg.CreateMap<Sign, SignDTO>().ForPath(dest => dest.**Items.Items**, opt => opt.MapFrom(src => src.**Items**));
As you can see in between the **, you're maping from an entity property to another entity children's property, this is what AutoMapper does not like and what you see in the error message.正如您在 ** 之间看到的那样,您正在从一个实体属性映射到另一个实体子属性,这是 AutoMapper 不喜欢的,也是您在错误消息中看到的。
Maybe it was a mistake on your side an you can removed one of the Items or you can create a new mapping for your first level of Items which takes care of the childrens.也许这是您的错误,您可以删除其中一个项目,或者您可以为您的第一级项目创建一个新的映射来照顾孩子。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.