Reputation: 69
I'm new in AutoMapper and I have problem with mapping my generic class to generic dto (using AutoMapper). I have this class:
public class Document<T>
{
public string Id { get; set; }
public Tag[] Tags { get; set; }
public T? Data { get; set; }
}
and this dto:
public class DocumentDto<T>
{
public string Id { get; set; }
public string[] Tags { get; set; }
public T? Data { get; set; }
}
and I need to make two-way mapping.. I created mapper profile in which I define this mapping like this:
public MapperProfile()
{
CreateMap<Document<FinancialReport>, DocumentDto<FinancialReportDto>>()
.ForMember(docDto => docDto.Tags, opt => opt.MapFrom(doc => doc.Tags.Select(tag => tag.Value).ToArray()));
CreateMap<DocumentDto<FinancialReportDto>, Document<FinancialReport>>()
.ForMember(docDto => docDto.Tags, opt => opt.MapFrom(doc => doc.Tags.Select(tag => new Tag { Value = tag }).ToArray()));
}
And then I setting this profile in extension method for dependency injection for IMapper:
public static IServiceCollection AddAutoMapper(this IServiceCollection services)
{
services.AddSingleton<IMapper>(sp =>
{
var config = new MapperConfiguration(cfg => {
cfg.AddProfile<MapperProfile>();
});
return config.CreateMapper();
});
return services;
}
And after this all when I try remap from DocumentDto=>Document or vice versa I got error: AutoMapperMappingException: Missing type map configuration or unsupported mapping.
I tryed googled for hours but nothing helped... Any ideas what I'm doing wrong?
UPDATE 1: I'm calling mapping like this:
public async Task<IResponse> UpdateFinancialReport(DocumentDto<FinancialReportDto> documentDto)
{
var doc = _mapper.Map<Document<FinancialReport>>(documentDto);
}
Upvotes: 0
Views: 464
Reputation: 69
ok, I found the problem.. In MapperProfile I was missing mapping config for generic types:
CreateMap<FinancialReport, FinancialReportDto>()
.ReverseMap();
Upvotes: 0