Automapper Relationship one for many - Model to Viewmodel (and vice versa)

Asked

Viewed 444 times

0

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

I’m trying to map one to many 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; }
}

So is my Viewmodel: (they look identical to Model, but they are not. To simplify, I omitted the Dataannotations and Methods, as they are not relevant to the solution of the problem)

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 class "Model To Viewmodel" I tried to create the mapping like this:

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

In the Obtencidadeporid method, I’m trying to return a Cidadeviewmodel:

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

Where Citizenmade Model represents a City and all data of the related State, according to consultation:

    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 Status property in Cidadeviewmodel is returning null. I believe the error is in the mapping (Createmap) in the class "Model To Viewmodel", but I can not solve the problem.

1 answer

1


Ola, Let’s see if I can help you. It’s possible you’re using the DDD architecture... Taking into account that you have an average knowledge of EF let’s possible solutions...

  1. "Problem: State property in Cidadeviewmodel is returning null. I believe the error is in the mapping (Createmap) in the "Model To Viewmodel" class, but I can’t solve it the problem"

    • Check that the table is correctly mapped.
    • Check whether the lazyload is enabled (otherwise, do not forget to
      call Include method on EF).
  2. Automapper
    • It is noticed that its modeling has a recursion that is not treated by the Automapper, for example: its class [State] has a collection [Cities] , already its class [City] has a type [State] This causes a recursion treated by the EF, but I couldn’t figure out how to fix it in the Automapper. This usually causes an overflow and processing aborts through an exception. Thus, it is advisable to review your modeling so that recursion does not occur.

I hope to have helped you and if you find another alternative please let me know.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.