C# 自动映射在反向映射中不工作

C# 自动映射在反向映射中不工作,c#,asp.net,asp.net-core-mvc,automapper,C#,Asp.net,Asp.net Core Mvc,Automapper,我有这些模型: 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

我有这些模型:

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; }
}
公共类下载请求{
公共int Id{get;set;}
公共日期时间日期{get;set;}=DateTime.Now;
公共bool已处理{get;set;}
公共字符串输出{get;set;}
public ICollection DownloadCategories{get;}=new HashSet();
}
公共类下载类别{
公共int Id{get;set;}
public int CategoryId{get;set;}
公共字符串CategoryName{get;set;}
公共虚拟int请求ID{get;set;}
公共虚拟下载请求{get;set;}
public ICollection DownloadDocuments{get;}=new HashSet();
}
公共类下载文档{
公共int Id{get;set;}
公共int文档{get;set;}
公共字符串路径{get;set;}
公共虚拟int类别ID{get;set;}
公共虚拟下载类别{get;set;}
}
公共类下载请求VM{
公共int Id{get;set;}
公共日期时间日期{get;set;}
公共bool已处理{get;set;}
公共字符串输出{get;set;}
public ICollection DownloadCategories{get;set;}=new HashSet();
公共字符串状态{
得到{
返回IsProcessed?“Processed”:“Processing”;
}
}
公共字符串重新分配{
得到{
返回string.Format(“{0:d/M/yyyy HH:mm:ss}”,日期);
}
}
}
公共类下载类别{
公共int Id{get;set;}
public int CategoryId{get;set;}
公共字符串CategoryName{get;set;}
public int RequestId{get;set;}
public ICollection DownloadDocuments{get;set;}=new HashSet();
}
公共类下载DocumentVM{
公共int文档{get;set;}
公共字符串路径{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));
cfg.CreateMap().ReverseMap();
cfg.CreateMap()
.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()
.ForMember(d=>d.DownloadCategories,opt=>opt.MapFrom(s=>s.DownloadCategories))
.ReverseMap()
.ForMember(d=>d.DownloadCategories,opt=>opt.MapFrom(s=>s.DownloadCategories));
当我尝试这种映射时,
mapper.Map(entities)
工作正常。但是这个
mapper.Map(request)
只映射
DownloadRequest
类,而不映射
DownloadDocuments
集合


如何修复映射配置以实现双向工作?

默认情况下,Automapper忽略没有设置程序的属性。在虚拟机上,您有
ICollection下载文档{get;set;}
,但在请求时,您只有
{get;}
。添加
集合

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>();
}
公共类下载请求{
公共int Id{get;set;}
公共日期时间日期{get;set;}=DateTime.Now;
公共bool已处理{get;set;}
公共字符串输出{get;set;}
公共ICollection下载类别{get;set;}
=新HashSet();
}