嵌套实体无效的自动映像

时间:2017-12-31 18:28:50

标签: asp.net-mvc automapper

我有两个课程,如下所述。我试图通过Auto Mapper映射它们,但它无法正常工作。

  public class VMTemplates
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Description { get; set; }
    public string Path { get; set; }
    public bool IsActive { get; set; }
    public System.DateTime TimeStamp { get; set; }
    public virtual ICollection<VMTemplateGroup> VMTemplateGroup { get; set; }
}

  public VMTemplateViewModel()
    {
        VMTemplateGroup = new List<VMTemplateGroupViewModel>();
    }
    public int Id { get; set; }

    [Required]
    public string Name { get; set; }
    [Required]
    public string Description { get; set; }
    public string Path { get; set; }
    public bool IsActive { get; set; }


    [Required(ErrorMessage = "Please Upload File")]
    [Display(Name = "Upload File")]
    [ValidateFile]
    public HttpPostedFileBase TemplateUpload { get; set; }

    public System.DateTime TimeStamp { get; set; }
    public List<VMTemplateGroupViewModel> VMTemplateGroup { get; set; }

我正在使用此代码来映射它们

     confi.CreateMap<VMTemplateViewModel, VMTemplates>().ReverseMap();
            confi.CreateMap<VMTemplateGroupViewModel, VMTemplateGroup>().ReverseMap();

调用代码

 public VMTemplates GetVMTemplateById(int id)
    {
        return DataContext.VMTemplates.Include("VMTemplateGroup").Where(a => a.Id == id).FirstOrDefault();

    }

这是异常发生的地方。我得到堆栈溢出异常

   public VMTemplateViewModel GetVMTemplateById(int templateId)
    {
        var result = _vmTemplateRepository.GetVMTemplateById(templateId);


        return _autoMapperService.MapEntity<VMTemplateViewModel>(result);
    }

0 个答案:

没有答案