简体   繁体   中英

Mapping to base class using automapper

I need to map from a source class to a destination base class with auto-mapper.

My scenario is as below.

class Source
{
    string FirstID { get; set; }
    string SecondID { get; set; }
}

Also my destination is as below

class DestinationBase
{
    string ID { get; set; }
}

class DestinationObject : DestinationBase
{
    string Prop { get; set; }
}

When I use automapper with the

Mapper.CreateMap<Source, DestinationObject>()
      .ForMember(d => d.ID, s => s.MapFrom(s.FirstID))
      .ForMember(d => d.ID, s => s.MapFrom(s.SecondID))

One of the ID after mapping does not work. Please any idea why ? I have tried include but i guess I don't understand its use well enough.

You're specifying the ID field in the destination object twice so the ID will be the value from s.SecondId.
What are you trying to accomplish with FirstID and SecondId in the destination object?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM