1

Please find code below:

public class CustomerDto
{  
    public int Id { get; set; }

    [Required]
    [StringLength(255)]
    public String Name { get; set; }
}

public class Customer
{  
    public int Id { get; set; }

    [Required]
    [StringLength(255)]
    public String Name { get; set; }
}

// ...

var customers = _context.Customers.ToList();
var cusDtos= Mapper.Map<List<Customer>,List<CustomerDto>>(customers);
return cusDtos;

All objects in cusDtos have value for all property except Id, Id is 0 for all objects. customers contains value for Id, but after mapping the Id becomes 0. Can anyone please help me to solve this issue?

Thanks in advance.

CSDev
  • 3,177
  • 6
  • 19
  • 37
Sandhya
  • 29
  • 5

2 Answers2

1

Have you tried to change this code

var cusDtos= Mapper.Map<List<Customer>,List<CustomerDto>>(customers);

change with this

var cusDtos= Mapper.Map<List<CustomerDto>>(customers);

and find the automapper profile. And change this

 CreateMap<Customer, CustomerDto>()
         .ForMember(m => m.Id, opt => opt.Ignore());

to this

 CreateMap<Customer, CustomerDto>();
Yigit Tanriverdi
  • 920
  • 5
  • 19
0

AutoMapper configuration:

CreateMap<Customer, CustomerDto>();

Your code:

var customers = _context.Customers.ToList();
var cusDtos = customers.Select(x => AutoMapper.Mapper.Map<CustomerDto>(x)).ToList();
return cusDtos;
Saeid Amini
  • 1,313
  • 5
  • 16
  • 26