"缺少从System.Char到System.String的映射" AutoMapper错误

时间:2017-01-17 22:56:02

标签: c# asp.net-mvc datatables automapper

我试图将Box对象映射到我的BoxedElectrodesRowModel

Box对象有一个属性SerialNumbers,每个那些都有自己的属性SerialNumberName。我尝试将SerialNumberName的列表映射到我的BoxedElectrodeRowModel SerialNumbers中的字符串列表。

AutoMapper代码

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

这是我现在得到的错误。我不知道&#34;字符&#34;它指的是。

  

缺少从System.Char到System.String的映射。创建使用   Mapper.CreateMap

所有这一切的目的是使用DataTables创建一个表,每个Box显示每行SerialNumbers的列表,如果有帮助的话。所有内容都已编码,但我在运行时会遇到上述错误。

编辑:这是我的课程映射。我尝试将BoxId映射到BoxId,将DateCreated映射到DateCreated,将SerialNumberName映射到每个SerialNumber(列表中) )SerialNumbers

方框(自动生成)

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

SerialNumber (这是每个&#39; Box&#39;的属性,也是自动生成的代码)

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

BoxedElectrodesRowModel

public class BoxedElectrodesRowModel
{
    public int BoxId { get; set; }
    public List<string> SerialNumbers { get; set; } // change from List to ICollection if there are problems
    public Nullable<System.DateTime> DateCreated { get; set; }
}

2 个答案:

答案 0 :(得分:0)

您的问题是FirstOrDefault()来电。因为它只从序列号名称返回第一个string。但字符串是IEnumerable<char>。当AutoMapper看到两个枚举时,它会尝试映射它们。在您的情况下,IEnumerable<char>IEnumerable<string>

要解决此问题,请删除FirstOrDefault()电话。因此,AutoMapper知道如何将IEnumerable<T>映射到List<T>,而不需要手动创建列表。

opts => opts.MapFrom(src => src.SerialNumbers.Select(t => t.SerialNumberName))

答案 1 :(得分:0)

我认为问题会在错误消息中突出显示。

Automapper被告知要从字符对象System.Char映射到字符串对象System.String。

我认为罪魁祸首是对FirstOrDefault()的调用,当调用字符串时,它将返回字符串中的第一个Char:

src.SerialNumbers.Select(t =&gt; t.SerialNumberName).FirstOrDefault()。ToList()

select语句从'SerialNumbers'集合中选择一个String,这是'MapFrom'调用所需的...所以你不需要调用.FirstOrDefault或.ToList()。

删除它们,看看情况如何:

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

还有另一篇关于这个问题的帖子:

AutoMapper: Collection to Single string Property