0

I have a Box object which has a list of SerialNumber objects nested within it. I'm trying to map the SerialNumberName property of each SerialNumber in each Box to a model called BoxedElectrodesModel.

Here is my Box class and its nested SerialNumber class:

public partial class Box
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Box()
    {
        this.SerialNumbers = new HashSet<SerialNumber>();
    }

    public int BoxID { get; set; }
    public System.DateTime DateCreated { get; set; }
    public Nullable<System.DateTime> DateShipped { get; set; }
    public string TrackingNumber { get; set; }
    public Nullable<System.DateTime> DateReceived { get; set; }
    public bool Active { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<SerialNumber> SerialNumbers { get; set; }
}

public partial class SerialNumber
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public SerialNumber()
    {
        this.Comments = new HashSet<Comment>();
        this.WIPHistories = new HashSet<WIPHistory>();
    }

    public int SerialNumberID { get; set; }
    public int IncomingLotID { get; set; }
    public string SerialNumber1 { get; set; }
    public string LamPurchaseOrder { get; set; }
    public string LamLineNumber { get; set; }
    public bool Refurbished { get; set; }
    public int WIPLocationID { get; set; }
    public int StatusID { get; set; }
    public int RouteSectionStepID { get; set; }
    public Nullable<int> RejectCategoryID { get; set; }
    public Nullable<int> BoxID { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<Comment> Comments { get; set; }
    public virtual IncomingLot IncomingLot { get; set; }
    public virtual RejectCategory RejectCategory { get; set; }
    public virtual Status Status { get; set; }
    public virtual WIPLocation WIPLocation { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<WIPHistory> WIPHistories { get; set; }
    public virtual Box Box { get; set; }
    public virtual RouteSectionStep RouteSectionStep { get; set; }
}

...and here is my 'BoxedElectrodesRowModel' class and its nested SerialNumberModel class:

public class BoxedElectrodesRowModel
{
    public int BoxId { get; set; }
    public List<SerialNumberModel> SerialNumbers { get; set; }
    public Nullable<System.DateTime> DateCreated { get; set; }
}

public class SerialNumberModel
{
    public string Name { get; set; }
}

Here's my AutoMapper code:

            c.CreateMap<Box, BoxedElectrodesRowModel>()
                .ForMember(dest => dest.BoxId, opts => opts.MapFrom(src => src.BoxID))
                .ForMember(dest => dest.SerialNumbers.Select(sn => sn.Name), opts => opts.MapFrom(src => src.SerialNumbers.Select(t => t.SerialNumberName)))
                .ForMember(dest => dest.DateCreated, opts => opts.MapFrom(src => src.DateCreated));

And here is my error message:

"Custom configuration for members is only supported for top-level individual members on a type."

The problem, I think, has something to do with the fact that I'm trying to map the nested property SerialNumberName from Box > SerialNumbers to the nested property Name from BoxedElectrodesRowModel > SerialNumbers.

How do I go about fixing this?

2
  • What version of automapper are you using? Commented Jan 20, 2017 at 0:09
  • @CodingYoshi version 5.1.1 Commented Jan 20, 2017 at 0:10

1 Answer 1

1

Try to map the the items SerialNumber and SerialNumberModel firstly and then AutoMapper will use that mapping when it is mapping from one list to another.

Mapper.Initialize( cfg =>
{
   cfg.CreateMap<SerialNumber, SerialNumberModel>()
      .ForMember( dest => dest.Name, opts => opts.MapFrom(src => src.SerialNumberName));
   cfg.CreateMap<Box, BoxedElectrodesRowModel>()
      .ForMember( dest => dest.BoxId, opts => opts.MapFrom( src => src.BoxID ) )
      .ForMember( dest => dest.DateCreated, opts => opts.MapFrom( src => src.DateCreated ) );
} );
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.