Automapper: Map a child list in nested object

  Kiến thức lập trình

I am trying to map PagesCategories but the code throws an exception:
Source entities:

   public class Pages 
{
    public Pages()
    {
        PagesLang = new HashSet<PagesLang>();
    }

    public int Order { get; set; }
    public string Tag { get; set; }



    public ICollection<PagesLang> PagesLang { get; set; }
    public ICollection<PagesCategory> PagesCategories { get; set; }

}


  public class PagesLang
{
    [Required]
    public string Culture { get; set; }
    [Required]
    public string Title { get; set; }
    [Required]
    public string Description { get; set; }


    public Guid PagesId { get; set; }
    public virtual Pages Pages { get; set; }
}


    public class PagesCategory : BaseEntity
{
    public Guid PagesId { get; set; }
    public Pages Pages { get; set; }
    public Guid CategoryId { get; set; }
    public Category Category { get; set; }

}

Destination

 public class PagesVM
{
    public Guid? BaseId { get; set; }

    [Display(Name = "Order number")]
    public int? Order { get; set; }
    [Required]
    [Display(Name = "Tag")]
    public string Tag { get; set; }

    [Display(Name = "Categories")]
    public string[] PagesCategoriesIds { get; set; }


    public IEnumerable<PagesCategoryVM> PagesCategories { get; set; }

}



public class PagesLangVM
{
    public Guid? BaseId { get; set; }

    [Required]
    [Display(Name = "Culture")]

    public string Culture { get; set; }
    [Required]
    [Display(Name = "Title")]

    public string Title { get; set; }
    [Required]
    [Display(Name = "Description")]

    public string Description { get; set; }

    public PagesVM Pages { get; set; }



}

public class PagesCategoryVM
{
    public Guid PagesId { get; set; }

    public Guid CategoryId { get; set; }
}

The mapping profile looks like this

    public class PagesProfile : Profile
{
    public PagesProfile()
    {
        CreateMap<PagesLangVM, Pages>().ReverseMap();
        CreateMap<PagesLangVM, PagesVM>().ReverseMap();
        CreateMap<PagesLangVM, PagesLang>().ReverseMap();

        CreateMap<PagesLang, ICollection<PagesCategoryVM>>().ReverseMap();
        CreateMap<PagesVM, ICollection<PagesCategoryVM>>().ReverseMap();
        CreateMap<Pages, ICollection<PagesCategoryVM>>().ReverseMap();
        CreateMap<PagesLangVM, ICollection<PagesCategoryVM>>().ReverseMap();

        CreateMap<Pages, PagesVM>().ReverseMap();
        //CreateMap<Pages, PagesCategoryVM>().ReverseMap();

    }

}

I am getting this error:

Error mapping types.

Mapping types:
List`1 -> IEnumerable`1
System.Collections.Generic.List`1[[Starter.DAL.Models.PagesLang, Starter.DAL, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]] -> System.Collections.Generic.IEnumerable`1[[Starter.Web.Areas.FrontendComplexPages.Models.PagesLangVM, Starter.Web, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]

LEAVE A COMMENT