I have a query that returns a client and its phone, but when mapping the class to the viewModel , I lose the phone data:
query:
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...
}