2017-03-20 3 views
1

で働いていない私は、これらのモデルを持っている:私は正常に動作mapper.Map<IEnumerable<DownloadRequest>, IEnumerable<DownloadRequestVM>>(entities)はこのマッピングをしようとすると、Automapperがリバースマッピング

cfg.CreateMap<DownloadDocument, DownloadDocumentVM>().ReverseMap(); 
      cfg.CreateMap<DownloadCategory, DownloadCategoryVM>() 
       .ForMember(d => d.DownloadDocuments, opt => opt.MapFrom(s => s.DownloadDocuments)) 
       .ReverseMap() 
       .ForMember(d => d.DownloadDocuments, opt => opt.MapFrom(s => s.DownloadDocuments)) 
       .ForMember(dest => dest.Request, opt => opt.Ignore()); 
      cfg.CreateMap<DownloadRequest, DownloadRequestVM>() 
       .ForMember(d => d.DownloadCategories, opt => opt.MapFrom(s => s.DownloadCategories)) 
       .ReverseMap() 
       .ForMember(d => d.DownloadCategories, opt => opt.MapFrom(s => s.DownloadCategories)); 

public class DownloadRequest { 
    public int Id { get; set; } 
    public DateTime Date { get; set; } = DateTime.Now; 
    public bool IsProcessed { get; set; } 
    public string Output { get; set; } 

    public ICollection<DownloadCategory> DownloadCategories { get; } = new HashSet<DownloadCategory>(); 
} 

public class DownloadCategory { 
    public int Id { get; set; } 
    public int CategoryId { get; set; } 
    public string CategoryName { get; set; } 
    public virtual int RequestId { get; set; } 
    public virtual DownloadRequest Request { get; set; } 

    public ICollection<DownloadDocument> DownloadDocuments { get; } = new HashSet<DownloadDocument>(); 
} 

public class DownloadDocument { 
    public int Id { get; set; } 
    public int Document { get; set; } 
    public string Path { get; set; } 
    public virtual int CategoryId { get; set; } 
    public virtual DownloadCategory Category { get; set; } 
} 

public class DownloadRequestVM { 
    public int Id { get; set; } 
    public DateTime Date { get; set; } 
    public bool IsProcessed { get; set; } 
    public string Output { get; set; } 
    public ICollection<DownloadCategoryVM> DownloadCategories { get; set; } = new HashSet<DownloadCategoryVM>(); 

    public string Status { 
     get { 
      return IsProcessed ? "Processed" : "Processing"; 
     } 
    } 

    public string RealDate { 
     get { 
      return string.Format("{0:d/M/yyyy HH:mm:ss}", Date); 
     } 
    } 
} 

public class DownloadCategoryVM { 
    public int Id { get; set; } 
    public int CategoryId { get; set; } 
    public string CategoryName { get; set; } 
    public int RequestId { get; set; } 
    public ICollection<DownloadDocumentVM> DownloadDocuments { get; set; } = new HashSet<DownloadDocumentVM>(); 
} 

public class DownloadDocumentVM { 
    public int Document { get; set; } 
    public string Path { get; set; } 
} 

私は以下の私のマッピングを設定できます。このmapper.Map<DownloadRequestVM, DownloadRequest>(request)DownloadRequestクラスのみをマッピングし、DownloadDocumentsコレクションはマッピングしません

両方の方法でマッピング設定を修正するにはどうすればよいですか?

答えて

1

Automapperは、デフォルトではセッターなしのプロパティを無視します。あなたのVMにはICollection<DownloadDocumentVM> DownloadDocuments { get; set; }がありますが、リクエストには{ get; }しかありません。追加する:set

public class DownloadRequest { 
    public int Id { get; set; } 
    public DateTime Date { get; set; } = DateTime.Now; 
    public bool IsProcessed { get; set; } 
    public string Output { get; set; } 

    public ICollection<DownloadCategory> DownloadCategories { get; set; } 
     = new HashSet<DownloadCategory>(); 
}