Automapper无法映射两个实体

时间:2015-09-30 09:05:32

标签: c# .net automapper

我试图使用Automapper映射两个实体 - ProductPictureDTO和ProductPictureBDO,但是我得到了一个例外,我无法理解你的错误是什么。

错误

  

缺少类型映射配置或不支持的映射。

     

映射类型:ProductPictureDTO - > GUID   ERP.DTO.Products.ProductPictureDTO - >的System.Guid

     

目的地路径:ProductPictureBDO

     

来源价值:ERP.DTO.Products.ProductPictureDTO

下面您可以看到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; }
}

下面是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; }

}

下面是尝试映射两个实体的方法

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

下面是负责映射的类

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();
    }
}

我在ProductMapper类的方法转换中得到了异常

1 个答案:

答案 0 :(得分:0)

Wild try :使用Mapper.DynamicMap而不是Mapper.Map (m冒险在这里投票!!)