简体   繁体   English

映射到嵌套值自动映射器

[英]Mapping to nested value Automapper

I'm struggling to map 2 objects. 我正在努力映射2个对象。 Basically have Product which is my EF model, and I'm mapping this to ProductDto, which has FileDto. 基本上有Product作为我的EF模型,我将其映射到具有FileDto的ProductDto。

I'd like to map Product.FileName to ProductDto.File.Internal name, how to do this? 我想将Product.FileName映射到ProductDto.File.Internal名称,该怎么做? Classes below. 下面的课程。

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

File Dto: 文件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 so far: 到目前为止的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();

I am able to map from ProductDto (on data post) to Product but not other way around, all help much appreciated 我能够从ProductDto(在数据发布中)映射到Product,但不能以其他方式映射,所有帮助都非常感谢

Thanks 谢谢

This code solved my issue: 这段代码解决了我的问题:

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

Applied to: 应用于:

CreateMap<Product, ProductDto>()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM