0

I have two classes which are mentioned below. I am trying to map those through Auto Mapper but it is not working.

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

I am using this code to map them

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

calling code

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

    }

This is where exception happens. I get stack overflow exception

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


        return _autoMapperService.MapEntity<VMTemplateViewModel>(result);
    }
user1037747
  • 1,307
  • 2
  • 18
  • 38
  • 1
    why do you mean by not working? have you tried adding try catch? is there any error? are some of the properties not mapping or all of them? could you post the line of code you are mapping ? – Dawood Awan Dec 31 '17 at 19:46
  • "I am using this code to map them"...that code just creates the mapping rule. Please also show how you actually perform the mapping when you have a specific instance of the objects. There are a few different ways to do it, so it might be significant. Also, do you get any errors or exceptions? Normally if automapper fails to map something it throws an exception. – ADyson Dec 31 '17 at 21:12
  • It says stack overflow exception. I have added calling code in the question – user1037747 Jan 01 '18 at 06:00
  • Try upgrading. If that doesn't solve it, make a proper repro. – Lucian Bargaoanu Jan 01 '18 at 06:26
  • What type is `_autoMapperService`? What is `result` populated with? – ADyson Jan 01 '18 at 09:52
  • This link resolved my issue https://stackoverflow.com/questions/37251043/automapper-throwing-stackoverflowexception-when-calling-projecttot-on-iquery – user1037747 Jan 01 '18 at 18:35

0 Answers0