我有一个Box
对象,其中嵌套有SerialNumber
个对象列表。我尝试将每个SerialNumberName
中每个SerialNumber
的{{1}}属性映射到名为Box
的模型。
以下是我的BoxedElectrodesModel
课程及其嵌套的Box
课程:
SerialNumber
...这是我的BoxedElectrodesRowModel' 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
。
我该如何解决这个问题?
答案 0 :(得分:1)
首先尝试映射项SerialNumber
和SerialNumberModel
,然后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 ) );
} );