Why does the model return null after using Automapper?

Asked

Viewed 103 times

1

To model Endereco returns null after mapping (Automapper), why ?

Example:
inserir a descrição da imagem aqui

  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?

  • Hello @Ricardo Punctual exactly ! You didn’t map anything ! I even edited the post and added a print of the running code.

  • 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

  • Thank you @Ricardo Pontual implemented your suggestion but this exception occurs: Error mapping types. Mapping types: Cliente -> ClienteDTO AutoMapperDemo.Cliente -> AutoMapperDemo.ClienteDTO &#xA;Type Map configuration: Cliente -> ClienteDTO AutoMapperDemo.Cliente -> AutoMapperDemo.ClienteDTO Destination Member: EnderecoDTO .

  • but after that line you kept the address mapper right? cfg.CreateMap<Endereco, EnderecoDTO>();

  • 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 ?

  • ready, responded to

Show 2 more comments

1 answer

2


It was not mapping correctness because, although it had been defined the mapper for address types (cfg.CreateMap<Endereco, EnderecoDTO>();), in class Cliente, the property is a List<Endereco>.

That’s why it’s important to inform Automapper how to correctly map the property Endereco, being like this:

Mapper.Initialize(cfg =>
            {
                cfg.CreateMap<Cliente, ClienteDTO>()
                   .ForMember(s => s.EnderecoDTO, c => c.MapFrom(m => m.Endereco));
                cfg.CreateMap<Endereco, EnderecoDTO>();
            });

Thus the Automapper knows how to map the type Endereco for EnderecoDTO and also knows that the member EnderecoDTO class ClienteDTO will be mapped from the member Endereco class Cliente.

Just one comment: as they are lists, I suggest using the property name in the plural, which also helps to distinguish from the class name:

public List<Endereco> Enderecos { get; set; }

and

public List<EnderecoDTO> EnderecosDTO { get; set; }

Browser other questions tagged

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