0

.ForMember(c = c.Property, options => options.Ignore()) works when you use a single object to map. However, the ignore does not work in the case of lists. For example see below code where destination List object already has some entries and when mapping, i want to ignore a specific property as per the created map. When the mapping happens, the ignore rules do not apply and the value gets set from source only

//class definition
 public class Customer
 {
 public int MyProperty { get; set; }
 public string Next { get; set; }
 }
 public class Customer2
 {
 public int MyProperty { get; set; }
 public string Next { get; set; }
 }

//create map where Next property is to be ignored
 AutoMapper.Mapper.CreateMap()
    .ForMember(c=> c.Next, options => options.Ignore());

//create and populate source list and dest list
 var sourceCust = new Customer() { MyProperty = 1, Next = string.Empty };
 var destCust = new Customer2() { MyProperty = 0, Next = "Hrishi" };
 var sourceList = new List<Customer>();
 sourceList.Add(sourceCust);

 var destList = new List<Customer2>();
 destList.Add(destCust);


//do the mapping
 destList = AutoMapper.Mapper.Map, List>(sourceList);

//now in the destCust instance, i expect Next to persist the value "Hrishi" based on options.ignore for property Next.. but it does not happen. this value gets set to null.

Joanvo
  • 5,677
  • 2
  • 25
  • 35
S2K
  • 1,185
  • 3
  • 27
  • 55

1 Answers1

0

In that sentence:

destList = AutoMapper.Mapper.Map, List>(sourceList);

Automapper is creating a new instance of List<Customer2> and asigning it to destList, so the previous object is not available anymore.

Joanvo
  • 5,677
  • 2
  • 25
  • 35