0

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); } 
5
  • 2
    can you share the code which tries to convert the document to dto and vice versa ? Commented Jan 27, 2023 at 2:55
  • @Chetan I editted end of post ;) Commented Jan 27, 2023 at 3:19
  • Can you provide the complete exception message for debugging purpose? Thanks. Commented Jan 27, 2023 at 3:53
  • @YongShun AutoMapper.AutoMapperMappingException: 'Error mapping types.' Inner exception: AutoMapperMappingException: Missing type map configuration or unsupported mapping. That's all what I get.. Commented Jan 27, 2023 at 4:18
  • Looks like the issue occurs while mapper is trying to convert Data property because mapping configuration is missing for FinancialReportDto and FinancialReport. So either you need to configure mapping for them or you should ignore converting Data property by doing .ForAllOtherMembers(opts => opts.Ignore()); Commented Jan 27, 2023 at 5:27

1 Answer 1

0

ok, I found the problem.. In MapperProfile I was missing mapping config for generic types:

CreateMap<FinancialReport, FinancialReportDto>() .ReverseMap(); 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.