1

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!.

Keko
  • 11
  • 1
  • 1
    Possible duplicate of [AutoMapper throwing StackOverflowException when calling ProjectTo() on IQueryable](http://stackoverflow.com/questions/37251043/automapper-throwing-stackoverflowexception-when-calling-projecttot-on-iquery) – Eugene Podskal Jul 02 '16 at 15:50
  • I think it is a little bit different because the relation is one to many. – Keko Jul 02 '16 at 16:34
  • Maybe, or maybe not. As far as I understand the issue is that Automapper doesn't memorize the entities it has already mapped, so **any** directly or indirectly selfreferencing data structure will cause such exception. Of course I can be absolutely wrong on that account, but that is a reasonable assumption. In any case that question has some suggestions that you should try and tell us why exactly they don't fit in your particular case. – Eugene Podskal Jul 02 '16 at 18:14

0 Answers0