自动映射器。缺少类型映射配置
AutoMapper. Missing type map configuration
我有单例 EntityMapper class 用于初始化映射器配置文件。
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();
}
在基本配置文件中 class:
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>)));
}
}
和child class:
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
class.[=19 的 实例 =]
但是,在您的 BaseMapperProfile
中,您调用了静态方法 Mapper.Map<Identity>(src)
。
但是在你的 EntityMapper
中你 configured/provided 所有映射仅用于 Mapper
的 实例 , 不适用于 static Mapper
.
要解决这个问题,您需要通过Mapper.Initialize
方法配置static Mapper
。
目前,我没有找到如何在其中传递映射器实例的方法(我认为它将在目前处于测试阶段的 5.0 版本中实现)。
我有单例 EntityMapper class 用于初始化映射器配置文件。
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();
}
在基本配置文件中 class:
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>)));
}
}
和child class:
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
class.[=19 的 实例 =]
但是,在您的 BaseMapperProfile
中,您调用了静态方法 Mapper.Map<Identity>(src)
。
但是在你的 EntityMapper
中你 configured/provided 所有映射仅用于 Mapper
的 实例 , 不适用于 static Mapper
.
要解决这个问题,您需要通过Mapper.Initialize
方法配置static Mapper
。
目前,我没有找到如何在其中传递映射器实例的方法(我认为它将在目前处于测试阶段的 5.0 版本中实现)。