I have the source and destination objects like this:
class ProductWithCategories // Source class
{
public Product Product { get; set; } // Product is an EF entity class
public IEnumerable<Category> Categories { get; set; }
}
class ProductViewModel // Dest class
{
public int Id { get; set; }
// Other properties with the same name as Product class
public IEnumerable<CategoryViewModel> Categories { get; set; }
}
So, my need is to map the values of source.Product
into dest
, and then source.Categories
into dest.Categories
. Is it possible with AutoMapper?
I have tried this and I was not surprised when it failed:
config.CreateMap<ProductWithCategories, ProductViewModel>()
.ForMember(q => q, option => option.MapFrom(q => q.Product))
.ForMember(q => q.Categories, option => option.MapFrom(q => q.Categories));
Here is the exception I received:
[AutoMapperConfigurationException: Custom configuration for members is
only supported for top-level individual members on a type.]
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…