AutoMapper Relationship One to Many - Model to ViewModel (and vice versa)

0

MVC scenario, where Controller talks to Application that talks to Domain.

I'm trying to do a one-to-many mapping with AutoMapper.

This is my Model:

public class Estado
{
    public Guid EstadoId { get; set; }
    public string Nome { get; set; }
    public string Sigla { get; set; }
    public string Capital { get; set; }

    public virtual ICollection<Cidade> Cidades { get; set; }
}

public class Cidade
{
    public Guid CidadeId { get; set; }
    public string Nome { get; set; }

    public Guid EstadoId { get; set; }
    public virtual Estado Estado { get; set; }
}

public class EstadoViewModel
{
    public Guid EstadoId { get; set; } = Guid.NewGuid();
    public string Nome { get; set; }
    public string Sigla { get; set; }
    public string Capital { get; set; }

    public virtual ICollection<CidadeViewModel> Cidades { get; set; }
}

public class CidadeViewModel
{
    public Guid CidadeId { get; set; } = Guid.NewGuid();
    public string Nome { get; set; }

    public Guid EstadoId { get; set; }
    public virtual EstadoViewModel Estado { get; set; }
}

In the Application in the "Model To ViewModel" class I tried to create the mapping like this:

CreateMap<Cidade, CidadeViewModel>()
    .ForMember(vm => vm.Estado, opt => opt.MapFrom(m => m.Estado)); 

In the GetPorId method, I'm trying to return a CidadeViewModel:

return Mapper.Map<Cidade, CidadeViewModel>(CidadeRetornadaDoModel);

Where ReturnModel City represents a City and all related State data, query:

    SELECT C.CidadeId, C.Nome, C.EstadoId, E.Nome, E.Sigla
      FROM Cidades AS C
INNER JOIN Estados AS E
        ON C.EstadoId = E.EstadoId
     WHERE C.CidadeId = 1

Problem: The State property in CityViewModel is returning null. I believe the error is in the mapping (CreateMap) in the "Model To ViewModel" class, but I can not solve the problem.

    
asked by anonymous 29.01.2017 / 18:10

1 answer

1

Hello, Let's see if I can help you. It is possible that you are using the DDD architecture ... Taking into account that you have an average knowledge of the EF we will the possible solutions ...

  • " Problem: The State property in CityViewModel is returning null. I believe the error is in the mapping (CreateMap) in the "Model To ViewModel" class, but I can not solve the problem "

    • Verify that the table is correctly mapped.
    • Check if lazyload is enabled (Otherwise, Do not forget about
      call the Include method in EF).
  • AutoMapper
    • It is noticed that its modeling has a recursion that is not handled by AutoMapper, for example: its [State] class has a collection [Cities], its [City] class already has a type [State] This causes a recursion handled by EF, but I did not figure out how to solve this in AutoMapper. This usually causes an overflow and processing aborts through an exception. Therefore, it is advisable to revise your modeling so that recursion does not occur.
  • I hope I have helped you and if you find any other alternative please let me know.

        
    22.03.2017 / 15:24