自动映射器的项目不能很好地使用泛型



ProjectTo与automapper和泛型一起使用似乎效果不佳。我创建了这个映射:

CreateMap<ChecklistSoftwareFirmware, OutputDTO>()
.AfterMap((entity, dto) => {
dto.ETag = "something here";
dto.Compiler = new() {
Name = "foo",
Version = "bar"
};
})

如果我这样做:

var entity = await _context.Set<TEntity>()
.AsNoTracking()
.Where(x => x.Uid == uid)
.ProjectTo<TOutputDTO>(_mapper.ConfigurationProvider)
.SingleOrDefaultAsync(cancellationToken);

其中TEntity是我的EF Core数据库模型,TOutputDTO是我的DTO类,AfterMap中没有任何东西运行。如果我这样做,它会正常工作:

var entity = await _context.Set<TEntity>()
.AsNoTracking()
.Where(x => x.Uid == uid)
.SingleOrDefaultAsync(cancellationToken);
var dto = _mapper.Map<TOutputDTO>(entity);

文档显示了ProjectTo支持的内容和不支持的内容的列表。

不支持CCD_ 6和CCD_。

并非所有映射选项都可以支持,因为生成的表达式必须由LINQ提供程序进行解释。AutoMapper 只支持LINQ提供商支持的内容

支持:

  • MapFrom(基于表达式(
  • ConvertUsing(基于表达式(
  • Ignore
  • NullSubstitute
  • 价值转换器
  • IncludeMembers

不支持:

  • Condition
  • SetMappingOrder
  • UseDestinationValue
  • MapFrom(基于函数(
  • Before/AfterMap
  • 自定义解析器
  • 自定义类型转换器
  • ForPath
  • 值转换器
  • 域对象上的任何计算属性

最新更新