I am trying to map two entities to its ViewModels, but something it is wrong because Visual Studio shows StackOveflowException.
Entities with EF6:
public partial class Users
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Users()
{
this.Mayorista = new HashSet<Mayorista>();
}
public int UserId { get; set; }
public string Name { get; set; }
public System.DateTime BirthDate { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Telephone { get; set; }
public string Email { get; set; }
public bool Active { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Mayorista> Mayorista { get; set; }
}
public partial class Mayorista
{
public int idMayorista { get; set; }
public int idUser { get; set; }
public string nombre { get; set; }
public virtual Users Users { get; set; }
}
ViewModels:
public class UsersViewModel
{
public int UserId { get; set; }
public string Name { get; set; }
public System.DateTime BirthDate { get; set; }
public string City { get; set; }
public string Country { get; set; }
public string Telephone { get; set; }
public string Email { get; set; }
public bool Active { get; set; }
public virtual ICollection<MayoristaViewModel> Mayorista { get; set; }
}
public class MayoristaViewModel
{
public int idMayorista { get; set; }
public int idUser { get; set; }
public string nombre { get; set; }
public virtual UsersViewModel Users { get; set; }
}
Implementation with Automapper:
var user = _service.GetById(id);
Mapper.Initialize(cfg => {
cfg.CreateMap<Users, UsersViewModel>();
cfg.CreateMap<Mayorista, MayoristaViewModel>();
});
var userView = Mapper.Map<Users, UsersViewModel>(user);
The Mapper initialize is correct, but when I do Mapper.Map, Visual Studio shows StackOverFlowException.
Thank you!.