尝试映射嵌套对象时出现Automapper错误

时间:2017-01-19 23:54:00

标签: c# asp.net-mvc entity-framework linq automapper

我有一个Box对象,其中嵌套有SerialNumber个对象列表。我尝试将每个SerialNumberName中每个SerialNumber的{​​{1}}属性映射到名为Box的模型。

以下是我的BoxedElectrodesModel课程及其嵌套的Box课程:

SerialNumber

...这是我的BoxedElectrodesRowModel&#39; 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; } } 类:

SerialNumberModel

这是我的AutoMapper代码:

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

以下是我的错误消息:

  

&#34;会员的自定义配置仅支持顶级   一个类型的个人成员。&#34;

我认为,这个问题与我尝试从 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)); &gt;映射嵌套属性SerialNumberName的事实有关。 Box来自SerialNumbers&gt;的嵌套属性Name BoxedElectrodesRowModel

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

首先尝试映射项SerialNumberSerialNumberModel,然后AutoMapper在从一个列表映射到另一个列表时将使用该映射。

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