Home > database >  Automapper returning null values inside object
Automapper returning null values inside object

Time:12-06

I am using automapper to map class object properties on my AWS lambda function. I am using automapper Profile to create mapping information based on the profile. I am not getting why this automapper is returning null for all the properties.

Here is the code:

private getOrder(Order order){
        var config = GetAutoMapperInstance();

        // getting null on all properties even on Line
        var myorder = config.Map<OrderDto>(order);
}

Here I am getting a Mapper Instance:

public static Mapper GetAutoMapperInstance()
    {
        MapperConfiguration config = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile(new MyCustomProfile());
        });

        return new Mapper(config);
    }

Here is my profile:

public MyCustomProfile()
        {
            CreateMap<Order, OrderDto>()
                .ForMember(dest => dest.LineDto, options => options.MapFrom(src => src.Lines))
                .ForAllMembers(option => option.Ignore());

            CreateMap<Lines, LineDto>()
                .ForMember(dest => dest.Id, options => options.MapFrom(src => src.ProductId))
                .ForMember(dest => dest.PriceIncTax, options => options.MapFrom(src => src.Price))
                .ForMember(dest => dest.Quantity, options => options.MapFrom(src => src.TotalQuantity))
                .ForAllMembers(option => option.Ignore());
}

Here are my classes

public class Order{
    public long? OrderId { get; set; }
    public long? UserId { get; set; }
    public string Email { get; set; }
    public IEnumerable<Lines> Lines { get; set; }
    public string Phone { get; set; }

}

public class Lines {
   public long ProductId { get; set; }
   public decimal? Price { get; set; }
   public int TotalQuantity { get; set; }

}

public class OrderDto{
   public long Id { get; set; }
   public long UserId { get; set; }
   public string Email { get; set; }
   public string PhoneNo { get; set; }
   public IEnumerable<LineDto> LineDto { get; set; }
}

public class LineDto {
   public long Id { get; set; }
   public decimal? PriceIncTax { get; set; }
   public int Quantity { get; set; }
   public List<LineDetail> LineDetails { get; set; }
}

CodePudding user response:

UPDATE: (as provided code in post was updated).

As @Lucian Bargaoanu suggested, you need to update your profile like this (replace ForAllMember to ValidateMemberList(MemberList.None)).

public class MyCustomProfile : Profile
{
    public MyCustomProfile()
    {
        CreateMap<Order, OrderDto>()
            .ForMember(dest => dest.LineDto, options => options.MapFrom(src => src.Lines))
            .ValidateMemberList(MemberList.None);

        CreateMap<Lines, LineDto>()
            .ForMember(dest => dest.Id, options => options.MapFrom(src => src.ProductId))
            .ForMember(dest => dest.PriceIncTax, options => options.MapFrom(src => src.Price))
            .ForMember(dest => dest.Quantity, options => options.MapFrom(src => src.TotalQuantity))
            .ValidateMemberList(MemberList.None);
    }
}
  • Related