2017-09-07 205 views
0

我很努力地映射2个对象。基本上有产品,这是我的EF模型,我将它映射到具有FileDto的ProductDto。映射到嵌套值Automapper

我想将Product.FileName映射到ProductDto.File.Internal名称,如何做到这一点? 下面的类。

public class Product : BaseEntity<long> 
    { 
    [MaxLength(100)] 
    public string Name { get; set; } 
    [MaxLength(100)] 
    public string Barcode { get; set; } 
    public int ShelfLife { get; set; } 
    public int Weight { get; set; } 
    public bool HasAllergens { get; set; } 
    [MaxLength(100)] 
    public string FileName { get; set; } 
    [ForeignKey("Id")] 
    public int CustomerId { get; set; } 
    public virtual ICollection<ProductIngredient> ProductIngredient { get; set; } 
    public virtual ICollection<Nutrition> Nutritions { get; set; } 
    public virtual ICollection<ProductComposition> Composition { get; set; } 
    public virtual IList<ProductionProcess> ProductionProcess { get; set; } 
    } 

    public class ProductDto 
    { 
    public long Id { get; set; } 
    public DateTime CretedOn { get; set; } 
    public DateTime UpdatedOn { get; set; } 
    public string Name { get; set; } 
    public string Barcode { get; set; } 
    public int ShelfLife { get; set; } 
    public int Weight { get; set; } 
    public bool HasAllergens { get; set; } 
    public int CustomerId { get; set; } 
    public FileDto File { get; set; } 
    public IList<IngredientDto> Ingredients { get; set; } 
    public IList<NutritionDto> Nutritions { get; set; } 
    public IList<ProductCompositionDto> Composition { get; set; } 
    public IList<ProductionProcessDto> ProductionProcess { get; set; } 

    } 

    public class ProductionProcessDto 
    { 
    public string Key { get; set; } 
    public string Value { get; set; } 
    public FileDto File { get; set; } 
    } 

    public class NutritionDto 
    { 
    public string Key { get; set; } 
    public string Value { get; set; } 
    } 

    public class ProductCompositionDto 
    { 
    public string Key { get; set; } 
    public string Value { get; set; } 
    } 

文件DTO:

public class FileDto 
    { 
    public string Base64EncodedFile { get; set; } 
    public string OriginalName { get; set; } 
    public string InternalName { get; set; } 
    public string Type { get; set; } 
    } 

Automapper至今:

//Product 
     CreateMap<Nutrition, NutritionDto>().ReverseMap(); 
     CreateMap<ProductComposition, ProductCompositionDto>().ReverseMap(); 
     CreateMap<ProductionProcessDto, ProductionProcess>() 
     .ForMember(dest => dest.FileInternalName, opt => opt.MapFrom(src => src.File.InternalName)) 
     .ForMember(dest => dest.FileOriginalName, opt => opt.MapFrom(src => src.File.OriginalName)) 
     .ReverseMap(); 

     CreateMap<Product, ProductDto>() 
     .ForMember(d => d.File, o => o.MapFrom(s => Mapper.Map<Product, FileDto>(s))) 
     .ForMember(d => d.Nutritions, o => o.MapFrom(s => s.Nutritions)) 
     .ForMember(d => d.Composition, o => o.MapFrom(s => s.Composition)) 
     .ForMember(d => d.ProductionProcess, o => o.MapFrom(s => s.ProductionProcess)) 
     .ForMember(d => d.Ingredients, o => o.MapFrom(s => s.ProductIngredient.Select(pi => pi.Ingredients))) 
     .ReverseMap(); 
     CreateMap<ProductDto, Product>() 
     .ForMember(d => d.FileName, o => o.MapFrom(s => s.File.InternalName)) 
     .ReverseMap(); 

我能够从ProductDto(数据后)映射到产品而不是其他方式,都有助于非常感谢

谢谢

回答

1

此代码解决了我的问题:

.ForMember(d => d.File, o => o.MapFrom(model => new FileDto { InternalName = model.FileName })) 

适用于:

CreateMap<Product, ProductDto>() 
+0

将其标记为正确答案 – Icet