Using ProjectTo
with automapper and generics doesn't seem to work well. I created this mapping:
CreateMap<ChecklistSoftwareFirmware, OutputDTO>()
.AfterMap((entity, dto) => {
dto.ETag = "something here";
dto.Compiler = new() {
Name = "foo",
Version = "bar"
};
})
If I do this:
var entity = await _context.Set<TEntity>()
.AsNoTracking()
.Where(x => x.Uid == uid)
.ProjectTo<TOutputDTO>(_mapper.ConfigurationProvider)
.SingleOrDefaultAsync(cancellationToken);
Where TEntity
is my EF Core database model and TOutputDTO
is my DTO class none of the stuff from AfterMap
runs. If I instead do this, it works properly:
var entity = await _context.Set<TEntity>()
.AsNoTracking()
.Where(x => x.Uid == uid)
.SingleOrDefaultAsync(cancellationToken);
var dto = _mapper.Map<TOutputDTO>(entity);