简体   繁体   English

Automapper不映射基础

[英]Automapper does not map base

Hi i am having some trouble making my mapping work in automapper. 嗨,我在使用automapper进行映射工作时遇到了一些麻烦。

I have 2 DTOs BaseDto and BaseOrganizationDto 我有2个DTOs BaseDto和BaseOrganizationDto

public class BaseDto
{}

public class SensitiveBaseDto : BaseDto
{}

I use the following mappings: 我使用以下映射:

CreateMap<IEntity, BaseDto>()
                .Include<IEntity, SensitiveBaseDto>()
                .IncludeBase<IEntity, BaseDto>();

I try to get a certain dto based on some logic like 我尝试根据某些逻辑来得到某个dto

public BaseDto MapToDto(Guid asSeenById, IEntity entity)
    if (entity.Id != asSeenById)
    {
      return this.MapToDto<BaseDto>(entity);
    }
    return this.MapToDto<SensitiveBaseDto>(entity);
}

But it always returns a SensitiveBaseDto, I have validated that the logic in the MapToDto method is executes properly. 但它总是返回一个SensitiveBaseDto,我已经验证了MapToDto方法中的逻辑是否正确执行。

What am I missing? 我错过了什么?

Solved it by doing this: 解决这个问题:

public override TDtoType MapToDto<TDtoType>(IEntity entity)
{
    var dto = typeof(TDtoType) == typeof(SensitiveDto) 
        ? new SensitiveDto() 
        : new BaseDto();

    this.Engine.Map(entity, dto);
    return dto as TDtoType;
}

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

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