Automapper无法映射两个实体

本文关键字:两个 实体 映射 Automapper | 更新日期: 2023-09-27 18:15:09

我试图映射两个实体- ProductPictureDTO与ProductPictureBDO -使用Automapper,但我得到一个异常,我无法理解什么是错误。

误差

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

映射类型:ProductPictureDTO -> Guiderp . dto . products . productpictureto -> 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

Automapper无法映射两个实体

的Convert方法中得到异常

狂野尝试:使用Mapper。DynamicMap而不是Mapper。地图