1
To model Endereco
returns null after mapping (Automapper), why ?
using AutoMapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AutoMapperDemo
{
class Program
{
static void Main(string[] args)
{
InitializeAutomapper();
Cliente cliente = new Cliente()
{
ClienteID = 1,
Nome = "João da Silva",
CPF = "111.888.999-99"
};
Endereco end1 = new Endereco();
end1.EnderecoID = 1;
end1.ClienteID = 1;
end1.Logradouro = "Rua XV de Novembro, 151";
end1.CEP = "01000-000";
cliente.Endereco.Add(end1);
Endereco end2 = new Endereco();
end2.EnderecoID = 2;
end2.ClienteID = 1;
end2.Logradouro = "Av. Interlagos, 1200";
end2.CEP = "03450-000";
cliente.Endereco.Add(end2);
var clienteDTO = Mapper.Map<Cliente, ClienteDTO>(cliente);
Console.WriteLine("Cliente ID::" + clienteDTO.ClienteID + ", Nome: " + clienteDTO.Nome + ", CPF: " + clienteDTO.CPF);
Console.WriteLine("Endereço Cliente 1 ID:" + clienteDTO.EnderecoDTO[0].EnderecoID + ", Logradouro: " + clienteDTO.EnderecoDTO[0].Logradouro + ", CEP: " + clienteDTO.EnderecoDTO[0].CEP);
Console.WriteLine("Endereço Cliente 2 ID:" + clienteDTO.EnderecoDTO[1].EnderecoID + ", Logradouro: " + clienteDTO.EnderecoDTO[1].Logradouro + ", CEP: " + clienteDTO.EnderecoDTO[1].CEP);
Console.ReadLine();
}
static void InitializeAutomapper()
{
Mapper.Initialize(cfg =>
{
cfg.CreateMap<Cliente, ClienteDTO>();
cfg.CreateMap<Endereco, EnderecoDTO>();
});
}
}
public class Cliente
{
public Cliente()
{
Endereco = new List<Endereco>();
}
public int ClienteID { get; set; }
public string Nome { get; set; }
public string CPF { get; set; }
public List<Endereco> Endereco { get; set; }
}
public class Endereco
{
public int EnderecoID { get; set; }
public int ClienteID { get; set; }
public string Logradouro { get; set; }
public string CEP { get; set; }
public Cliente Cliente { get; set; }
}
public class ClienteDTO
{
public int ClienteID { get; set; }
public string Nome { get; set; }
public string CPF { get; set; }
public List<EnderecoDTO> EnderecoDTO { get; set; }
}
public class EnderecoDTO
{
public int EnderecoID { get; set; }
public int ClienteID { get; set; }
public string Logradouro { get; set; }
public string CEP { get; set; }
public ClienteDTO ClienteDTO { get; set; }
}
}
did not map anything, including Clienteid?
– Ricardo Pontual
Hello @Ricardo Punctual exactly ! You didn’t map anything ! I even edited the post and added a print of the running code.
– hard123
by your print not mapped only the right address list? try setting the mapper so:
cfg.CreateMap<Cliente, ClienteDTO>().ForMember(s => s.EnderecoDTO, c => c.MapFrom(m => m.Endereco));
see if it maps correctly and can put the explaining syntax in a reply– Ricardo Pontual
Thank you @Ricardo Pontual implemented your suggestion but this exception occurs:
Error mapping types. Mapping types: Cliente -> ClienteDTO AutoMapperDemo.Cliente -> AutoMapperDemo.ClienteDTO 
Type Map configuration: Cliente -> ClienteDTO AutoMapperDemo.Cliente -> AutoMapperDemo.ClienteDTO Destination Member: EnderecoDTO
.– hard123
but after that line you kept the address mapper right?
cfg.CreateMap<Endereco, EnderecoDTO>();
– Ricardo Pontual
I had removed why it didn’t work, so I added it and it worked perfectly ! Thanks ! You can reply separately to the solution so I finish the post and mark as sure ?
– hard123
ready, responded to
– Ricardo Pontual