在基于ASP.NET MVC和EF的Angular项目中,我具有以下实体,如下所示:
注意:如果我不添加文件附件,则没有错误。但是添加文件会在映射过程中导致此错误。
Ticket.cs:
public class Ticket
{
[Key]
public int Id { get; set; }
public string Summary { get; set; }
public string Description { get; set; }
//code omitted for brevity
public virtual ICollection<FileAttachment> FileAttachments { get; set; }
}
TicketViewModel.cs:
public class TicketViewModel
{
public int Id { get; set; }
//code omitted for brevity
public virtual ICollection<FileAttachmentViewModel> FileAttachments { get; set; }
}
FileAttachmentViewModel.cs:
public class FileAttachmentViewModel
{
public int Id { get; set; }
public int TicketId { get; set; }
public string FileName { get; set; }
public byte[] FileData { get; set; }
//code omitted for brevity
public virtual Ticket Ticket { get; set; }
}
这里是我遇到“错误映射类型。映射类型:TicketViewModel-> Ticket ...”错误的映射和Save方法。
Service.cs:
public TicketViewModel Save(TicketViewModel model)
{
//here I encounter the error while mapping TicketViewModel to Ticket
var record = model.MapEntity<Ticket>();
//code omitted for brevity
}
Mapping.cs:
CreateMap<Ticket, TicketViewModel>();
CreateMap<TicketViewModel, Ticket>()
.ForMember(dest => dest.FileAttachments, opts => opts.MapFrom(src => src.FileAttachments);
CreateMap<FileAttachment, FileAttachmentViewModel>();
CreateMap<FileAttachmentViewModel, FileAttachment>();
任何帮助将不胜感激...
答案 0 :(得分:0)
使用至少自己定义的属性FileAttachments的Ticket类的定义也将很有帮助。 它可能与FileAttachments属性类型是一个接口有关。您没有FileAttachmentViewModel类型的任何映射,这是将一个通用集合映射到另一个通用集合的要求。我假设您在Ticket类中具有FileAttachment类型的属性,而不是FileAttachmentViewModel类型的属性。因此,AutoMapper会给您一个例外。