【问题标题】:Automapper not working in Reverse MappingAutomapper 在反向映射中不起作用
【发布时间】:2017-03-21 04:55:48
【问题描述】:

我有这些模型:

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

我已经在下面配置了我的映射:

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

当我尝试此映射时,mapper.Map&lt;IEnumerable&lt;DownloadRequest&gt;, IEnumerable&lt;DownloadRequestVM&gt;&gt;(entities) 工作正常。但是这个mapper.Map&lt;DownloadRequestVM, DownloadRequest&gt;(request) 只映射DownloadRequest 类而不是DownloadDocuments 集合

如何修复我的映射配置以双向工作?

【问题讨论】:

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


    【解决方案1】:

    Automapper 默认忽略没有设置器的属性。在您的 VM 上,您有 ICollection&lt;DownloadDocumentVM&gt; 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>();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-08
      • 1970-01-01
      • 2017-07-23
      • 1970-01-01
      • 2021-08-11
      • 1970-01-01
      • 1970-01-01
      • 2017-03-13
      相关资源
      最近更新 更多