2

I have an object (ProductModel) that has a nested list of images. I am trying to simplify the model (Product) that has this list as its property. I am using Automapper, but I can not seem to get the mapping configuration right. I viewed several other posts, but they seem to be a little different than what I am trying to achieve.

// Map to:
public class Product
{
    public List<Image> Images { get; set; }
}

public class Image
{
    public string url { get; set; }
}


// Map from:
public class ProductModel
{
    public ImageSet ImageSet { get; set; }
}

public class ImageSet
{
    public List<ImageDetail> ImageDetails { get; set; }
}

public class ImageDetail
{
    public string Url { get; set; }
}
3
  • you need to have the same name on the properties or create a mapping confiugration Commented Mar 1, 2017 at 3:56
  • Yes, it's the configuration that I'm trying to figure out because the objects can not change. Commented Mar 1, 2017 at 5:25
  • What version of automatter for EF Core or 4? Commented Mar 1, 2017 at 5:48

1 Answer 1

2

The following configuration should work:

var config = new MapperConfiguration(cfg =>
{
    cfg.CreateMap<ImageDetail, Image>();
    cfg.CreateMap<ProductModel, Product>()
        .ForMember(dest => dest.Images, opt => opt.MapFrom(src => src.ImageSet.ImageDetails))
        ;
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.