Automapper fails to map two entities

梦想的初衷 提交于 2019-12-25 03:04:09

问题


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


回答1:


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



来源:https://stackoverflow.com/questions/32862099/automapper-fails-to-map-two-entities

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!