自动映射实体到模型

AutoMapping Entity to Model

我试图映射这些但我得到

AutoMapper.AutoMapperMappingException: 'Missing type map configuration or unsupported mapping.'

public class MenuItemViewModel
{
    [Required]
    public string Text { get; set; }
    public string URL { get; set; }
    public string Icon { get; set; }
    public IList<MenuItemViewModel> Children { get; set; }
    public MenuItemViewModel Parent { get; set; }
    public int Sort { get; set; }
}

public class MenuItem
{
    public Guid Id { get; set; }
    public string Text { get; set; }
    public string URL { get; set; }
    public string Icon { get; set; }
    public IList<MenuItem> Children { get; set; }
    public MenuItem Parent { get; set; }
    public bool onMenu { get; set; }
    public int Sort { get; set; }

    public IList<MenuItemPermittedRole> MenuItemPermittedRoles { get; set; }
}

Mapper.Initialize(cfg => {
    cfg.CreateMap<MenuItem, MenuItemViewModel>().ReverseMap();
});

MenuItemViewModel result = Mapper.Map<MenuItemViewModel>(menuItems); ERROR HERE

如果不知道您收到的是哪个错误,很难回答,但我可以猜到...如果 menuItems 是 IEnumerable<MenuItem>,您应该写

var result = Mapper.Map<IEnumerable<MenuItemViewModel>>(menuItems);