0

I'm trying to map two entities - ProductPictureDTO with ProductPictureBDO - using the Automapper, but I get an exception and I'm not able to understand what is thew mistake.

The error

Missing type map configuration or unsupported mapping.

Mapping types: ProductPictureDTO -> Guid ERP.DTO.Products.ProductPictureDTO -> System.Guid

Destination path: ProductPictureBDO

Source value: ERP.DTO.Products.ProductPictureDTO

Here below you can see ProductPictureDTO

[DataContract]
public class ProductPictureDTO
{
    [DataMember]
    public Guid ProductPictureId { get; private set; }

    [DataMember]
    public byte[] Image { get; set; }

    [DataMember]
    public Guid ProductId { get; set; }

    [DataMember]
    public virtual ProductDTO Product { get; set; }
}

Here below the properties of ProductPictureBDO

public class ProductPictureBDO : IEntity<ProductPictureBDO>, IValidation
{
    public const string PICTURE_CONTENT_NOT_VALID_ERROR_MESSAGE = "Picture: the content of the picture is not valid";
    public const string ASSOCIATED_PRODUCT_NOT_VALID_ERROR_MESSAGE = "Picture: the picture is not associated to a product";

    public ProductPictureBDO(Guid id)
    {
        ProductPictureId = id;
    }

    public Guid ProductPictureId { get; private set; }

    public byte[] Image { get; set; }

    public bool Deleted { get; private set; }

    #region Navigation properties

    public virtual ProductBDO Product { get; set; }

    public Guid ProductId { get; set; }

}

Here below the method which tries to map the two entities

public IEnumerable<IError> ValidateProductPicture(ProductPictureDTO picture)
{
    return _productsManager.ValidateProductPicture(ProductsMapper.Convert<ProductPictureDTO, ProductPictureBDO>(picture));
}

Here below the class responsible for mapping

public static class ProductsMapper
{
    static ProductsMapper()
    {
        MapProductBDOToDTO();
        MapProductDTOToBDO();

        MapProductCategoryBDOToDTO();
        MapProductCategoryDTOToBDO();

        MapIvaBDOToDTO();
        MapIvaDTOToBDO();

        MapProductSupplierBDOToDTO();
        MapProductSupplierDTOToBDO();

        MapProductPictureBDOToDTO();
        MapProductPictureDTOToBDO();

        MapProductNoteBDOToDTO();
        MapProductNoteDTOToBDO();

        MapStockProductBDOToDTO();
        MapStockProductDTOToBDO();

        MapTagBDOToDTO();
        MapTagDTOToBDO();
    }


    public static TTargetType Convert<TToConvert, TTargetType>(TToConvert toConvert)
    {

        return Mapper.Map<TTargetType>(toConvert);
    }


    private static void MapProductDTOToBDO()
    {
        Mapper.CreateMap<ProductDTO, ProductBDO>();
    }

    private static void MapProductBDOToDTO()
    {
        Mapper.CreateMap<ProductDTO, ProductBDO>().ReverseMap();
    }

    private static void MapProductCategoryDTOToBDO()
    {
        Mapper.CreateMap<ProductCategoryDTO, ProductCategoryBDO>();
    }

    private static void MapProductCategoryBDOToDTO()
    {
        Mapper.CreateMap<ProductCategoryBDO, ProductCategoryDTO>();
    }

    private static void MapIvaDTOToBDO()
    {
        Mapper.CreateMap<IvaDTO, IvaBDO>();
    }

    private static void MapIvaBDOToDTO()
    {
        Mapper.CreateMap<IvaBDO, IvaDTO>();
    }

    private static void MapProductSupplierDTOToBDO()
    {
        Mapper.CreateMap<ProductSupplierDTO, ProductSupplierBDO>();
    }

    private static void MapProductSupplierBDOToDTO()
    {
        Mapper.CreateMap<ProductSupplierDTO, ProductSupplierBDO>().ReverseMap();
    }

    private static void MapProductPictureDTOToBDO()
    {
        Mapper.CreateMap<ProductPictureDTO, ProductPictureBDO>();
    }

    private static void MapProductPictureBDOToDTO()
    {
        Mapper.CreateMap<ProductPictureDTO, ProductPictureBDO>().ReverseMap();
    }

    private static void MapProductNoteDTOToBDO()
    {
        Mapper.CreateMap<ProductNoteDTO, ProductNoteBDO>();
    }

    private static void MapProductNoteBDOToDTO()
    {
        Mapper.CreateMap<ProductNoteDTO, ProductNoteBDO>().ReverseMap();
    }

    private static void MapStockProductDTOToBDO()
    {
        Mapper.CreateMap<StockProductDTO, StockProductBDO>();
    }

    private static void MapStockProductBDOToDTO()
    {
        Mapper.CreateMap<StockProductDTO, StockProductBDO>().ReverseMap();
    }

    private static void MapTagDTOToBDO()
    {
        Mapper.CreateMap<TagDTO, TagBDO>();
    }

    private static void MapTagBDOToDTO()
    {
        Mapper.CreateMap<TagDTO, TagBDO>().ReverseMap();
    }
}

I get the exception in the method Convert of the class ProductMapper

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Errore Fatale
  • 978
  • 1
  • 9
  • 21

1 Answers1

0

Wild try : use Mapper.DynamicMap instead of Mapper.Map (m risking the down votes here!!)

Nikita Shrivastava
  • 2,978
  • 10
  • 20
  • Thanks for the answer, but in reality the Autompper is able to map private properties. – Errore Fatale Sep 30 '15 at 09:24
  • It works, but it seems to me that this is "climbing over the problem" and not "fighting the problem". Can you explain what is DynamicMap and why do you suggest it? As far as I know, DynamiacMap should be used only when you don't know the types at compile time: you don't have to create the various mappings. But this means that the mapping will be performed "on the fly" each time you want to map a type, which is not efficient. – Errore Fatale Sep 30 '15 at 09:51
  • I believe it happened because you have multiple CreateMap calls inside the ProductsMapper ,which should be ideally only once inside an app domain & is resulting race condition for you.When you change the Map to dynamic map it certainly bypasses all the configuration. this is my depiction based on my half an hr of study. – Nikita Shrivastava Sep 30 '15 at 10:06
  • First of all, I don't see why the CreateMap calls should be a problem, since they are all different. Second, the mappings are created inside a static constructor, which by definition is called only ONCE (not more than ONCE) during the session. Third: although creating the same mapping more than one time is for sure not efficient, I don't think that it can cause exceptions. – Errore Fatale Sep 30 '15 at 10:23
  • See the following example: http://pastebin.com/VJFxwzLx No exceptions are thrown – Errore Fatale Sep 30 '15 at 10:30
  • @ErroreFatale http://shahvaibhav.com/solving-missing-type-map-configuration-or-unsupported-mapping-error-of-automapper-in-c/ problem identical to yours. – Nikita Shrivastava Oct 01 '15 at 02:13