Automapper无法映射两个实体 [英] Automapper fails to map two entities
问题描述
我试图映射两个实体 - ProductPictureDTO与ProductPictureBDO - 使用Automapper,但我得到一个例外,我不能够明白什么是thew错
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.
错误
缺少类型映射配置或不支持的映射。
Missing type map configuration or unsupported mapping.
映射类型:ProductPictureDTO - >的Guid ERP.DTO.Products.ProductPictureDTO - >的System.Guid
Mapping types: ProductPictureDTO -> Guid ERP.DTO.Products.ProductPictureDTO -> System.Guid
目标路径:ProductPictureBDO
Destination path: ProductPictureBDO
来源值:ERP.DTO.Products.ProductPictureDTO
Source value: ERP.DTO.Products.ProductPictureDTO
下面下面你可以看到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; }
}
下面下面ProductPictureBDO的属性
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));
}
下面下面负责测绘类
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的方法转换
I get the exception in the method Convert of the class ProductMapper
推荐答案
野生尝试的:使用Mapper.DynamicMap代替Mapper.Map (M冒着向下票在这里!)
Wild try : use Mapper.DynamicMap instead of Mapper.Map (m risking the down votes here!!)
这篇关于Automapper无法映射两个实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!