汽车应用程序.缺少类型地图配置



我有singleton entitymapper类,用于初始化映射器配置文件。

    private readonly IMapper _mapper;
    private static EntityMapper _instance;
    public static EntityMapper Instance => _instance ?? (_instance = new EntityMapper());
    private EntityMapper()
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.AddProfile(new ViewColumnMapperProfile());
        });
        _mapper = config.CreateMapper();
    }

在基本概况类中:

public class BaseMapperProfile<TSorce, TTarget> : Profile
{
    protected override void Configure()
    {
        CreateMap<List<TSorce>, IdentityList>()
            .ForCtorParam("identities", opt => opt.MapFrom(src => Mapper.Map<Identity>(src)));
        CreateMap<IdentityList, List<TSorce>>()
            .ConstructUsing(scr => new List<TSorce>(scr.Select(Mapper.Map<TSorce>)));
    }
}

和儿童课:

public class ViewColumnMapperProfile : BaseMapperProfile<AP_ViewColumn, ColumnInfo>
{
    protected override void Configure()
    {
        CreateMap<AP_ViewColumn, ColumnInfo>()
            .ForMember(...)
        CreateMap<ColumnInfo, AP_ViewColumn>()
            .ForMember(...)
        CreateMap<AP_ViewColumn, Identity>()
            .ForMember(...)
        CreateMap<Identity, AP_ViewColumn>()
            .ForMember(...);
        base.Configure();
    }
}

然后我使用映射:

    var res = EntityMapper.Map<List<AP_ViewColumn>>(identityList);

此代码引发异常:缺少类型的MAO配置或不支持的Mappig。

映射类型: 身份 -> ap_viewcolumn

但是当我使用时:

Mapper.Initialize(cfg => cfg.AddProfile(new ViewColumnMapperProfile())); 
var res = EntityMapper.Map<List<AP_ViewColumn>>(identityList);

它可以

您已经在问题上有答案。

使用var res = EntityMapper.Map<List<AP_ViewColumn>>(identityList);时,您会使用自己的包装器,该包装器配置并使用Mapper类的实例

但是,在您的BaseMapperProfile内部,您可以呼叫静态方法Mapper.Map<Identity>(src)

但是,在EntityMapper中,您仅适用/提供了所有映射,仅适用于 instance of Mapper不适合 static Mapper

要解决您需要通过Mapper.Initialize方法配置static Mapper

目前,我还没有找到如何通过映射器实例的方式(我认为它将以5.0版本实现,该版本当前在beta中)。

最新更新