简体   繁体   English

使用自动映射器映射到基类

[英]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 当我将automapper与

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. 映射后的ID之一不起作用。 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. 您要在目标对象中指定两次ID字段,因此ID将是s.SecondId中的值。
What are you trying to accomplish with FirstID and SecondId in the destination object? 您要在目标对象中使用FirstID和SecondId完成哪些操作?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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