3
I have an appointment that returns a client and his phone, but when mapping the class to the viewModel, I lose phone data:
consultation:
public Cliente ObterClientePorId(Guid ClienteId)
{
var consulta =
Contexto.Cliente.Join(Contexto.Telefone,
p => p.ClienteId,
pt => pt.ClienteId,
(p, pt) => new {p, pt}).FirstOrDefault();
if (consulta != null)
{
var cliente = consulta.p;
cliente.telefone = consulta.pt;
return cliente;
}
But when mapping the client class to the viewModel cliente
, it loses the data that was in telefone
.
Mapping:
public ClienteViewModel ObterClientePorId(Guid ClienteId)
{
//aqui tem os dados de telefone
var consulta = _cliente.ObterClientePorId(ClienteId);
//aqui o telefone fica null
var cliente = Mapper.Map<Cliente, ClienteViewModel>(consulta);
return cliente;
}
ClienteViewModel
:
public class ClienteViewModel
{
public ClienteViewModel()
{
ClienteId = Guid.NewGuid();
}
// campos...
public List<TelefoneViewModel> TelefoneViewModel { get; set; }
//mais campos...
}
Already tried not to use Automapper?
– Leonel Sanches da Silva
I didn’t try, then I would do the hand mapping?
– Aesir
Yes, using implicit operators, for example.
– Leonel Sanches da Silva