Automapper with base class and different configuration options for implementations

后端 未结 2 1732
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-12 10:36

I have two classes (MVC view model) which inherits from one abstract base class.

abstract class BaseModel { }

class Car : BaseModel 
{
    public string Speed {         


        
相关标签:
2条回答
  • 2021-02-12 10:57

    Here is the topic describing Mapping Inheritance.

    The following should work for you:

    Mapper.CreateMap<BaseModel, DataDastination>()
        .Include<Car, DataDastination>()
        .Include<Camper, DataDastination>();//.ForMember(general mapping)
    Mapper.CreateMap<Car, DataDastination>();//.ForMember(some specific mapping)
    Mapper.CreateMap<Camper, DataDastination>();//.ForMember(some specific mapping)
    
    0 讨论(0)
  • 2021-02-12 11:05

    Use .IncludeAllDerived()

    Mapper.CreateMap<BaseModel, DataDestination>().IncludeAllDerived()
    Mapper.CreateMap<Car, DataDestination>();
    Mapper.CreateMap<Camper, DataDestination>();
    
    0 讨论(0)
提交回复
热议问题