【问题标题】:Mapping to nested value Automapper映射到嵌套值 Automapper
【发布时间】:2017-09-07 09:46:17
【问题描述】:

我正在努力映射 2 个对象。基本上有 Product,这是我的 EF 模型,我将它映射到 ProductDto,它有 FileDto。

我想将 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; }
  }

文件目录:

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

到目前为止的自动映射器:

//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(在数据发布上)映射到 Product,但不能以其他方式映射,非常感谢所有帮助

谢谢

【问题讨论】:

    标签: c# .net-core automapper


    【解决方案1】:

    这段代码解决了我的问题:

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

    适用于:

    CreateMap<Product, ProductDto>()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-17
      • 2017-07-20
      • 2020-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多