2017-09-07 6 views
0

Ich habe Mühe, 2 Objekte zu mappen. Im Grunde haben Sie Produkt, das mein EF-Modell ist, und ich mappe das zu ProductDto, das FileDto hat.Mapping auf verschachtelten Wert Automapper

Ich möchte Product.FileName zu ProductDto.File.Internal Name zuordnen, wie geht das? Klassen unten.

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

Datei 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 bisher:

//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(); 

ich in der Lage bin von ProductDto (auf Daten post) zu Produkt abzubilden, aber nicht umgekehrt, alle Hilfe sehr geschätzt

Danke

Antwort

1

Dieser Code löste mein Problem:

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

Angewandt auf:

CreateMap<Product, ProductDto>() 
+0

Mark es als richtige Antwort – Icet