AutoMapper updated from 3 to 4 broken inheritance maps

I upgraded AutoMapper from 3.3.1 to 4.0.4, which broke the following mapping to this post

Cannot overlay object of type "Foo" to type "BarDTO"

Classes

public class FooDTO { // omitted } // derived DTO public class BarDTO : FooDTO { public string Extra { get; set; } } 

Mapping configuration

 Mapper.CreateMap<Foo, FooDTO>().ReverseMap(); Mapper.CreateMap<Foo, BarDTO>(); 

Mapping

 Map<Foo, BarDTO>(foo); // throws cast exception 

I also tried using the .Include() method, but it didn't make any difference.

 Mapper.CreateMap<Foo, FooDTO>() .Include<Foo, BarDTO>() .ReverseMap(); Mapper.CreateMap<Foo, BarDTO>(); 

Am I doing something wrong or is this a mistake?

+4
source share
1 answer

This known change occurred from 3.xx to 4. Customizing the display inside Mapper.Initialize will solve the problem.

eg. The mapping 3.xx is performed as follows:

 Mapper.CreateMap<Order, OrderDto>() .Include<OnlineOrder, OnlineOrderDto>() .Include<MailOrder, MailOrderDto>(); Mapper.CreateMap<OnlineOrder, OnlineOrderDto>(); Mapper.CreateMap<MailOrder, MailOrderDto>(); 

The 4.xx mapping should now be done in the Initialize method using the delegate.

 Mapper.Initialize(cfg => { cfg.CreateMap<Order, OrderDto>() .Include<OnlineOrder, OnlineOrderDto>() .Include<MailOrder, MailOrderDto>(); cfg.CreateMap<OnlineOrder, OnlineOrderDto>(); cfg.CreateMap<MailOrder, MailOrderDto>(); }); 

Here the discussion is related to issue .

Ver update: bug fixed for step 4.1.0

Alternatively, you can print images.

 Mapper.Configuration.Seal(); 
+3
source

All Articles