AutoMapper/AutoMapper — Issue #145: Error when creating maps in wrong order with inheritance
Author: Jarlen007
Created: 2011-11-09 11:27:57+00:00
State: open

When trying to map with inheritance as described in Wiki section but add a test field (not property) the second "CreateMap" will return an error "Sequence contains more than one matching element".

Class setup:

public class Order { public string tst; }
public class OnlineOrder : Order { public string tst2; }
public class MailOrder : Order { public string tst3; }

public class OrderDto { public string tst; }
public class OnlineOrderDto : OrderDto { public string tst2; }
public class MailOrderDto : OrderDto { public string tst3; }

Do the mapping:

Mapper.CreateMap()
.Include()
.Include();
Mapper.CreateMap();
Mapper.CreateMap();

var order = new OnlineOrder();
order.tst = "tst1";
order.tst2 = "tst2";
var mapped = Mapper.Map(order, order.GetType(), typeof(OrderDto));

This will make "Mapper.CreateMap();" throw an exception.

Workaround is to move the mappings for the overriden classes above OR make the fields in the classes to properties instead(!) which then will work in the order above.