Missing type map configuration or unsupported mapping.while trying to assign the data to DTO object

720 views Asked by At

I am using AutoMapper.Extensions.Microsoft.DependencyInjection. I am using the Automapper NuGet package: AutoMapper.Extensions.Microsoft.DependencyInjection (7.0.0) for ASP.NET Core 3.1 application.

Here goes my domain object: file ResourceGroup.cs

public class ResourceGroups
{
    public string id
    {
        get;
        set;
    }

    public int ResourceGroupId
    {
        get;
        set;
    }

    public bool IsPublished
    {
        get;
        set;
    }

    public int? Position
    {
        get;
        set;
    }

    public string CreatedBy
    {
        get;
        set;
    }

    public string CreatedDate
    {
        get;
        set;
    }

    public string UpdatedBy
    {
        get;
        set;
    }

    public string UpdatedDate
    {
        get;
        set;
    }

    public int? ResourceGroupContentId
    {
        get;
        set;
    }

    public int LanguageId
    {
        get;
        set;
    }

    public string GroupName
    {
        get;
        set;
    }
}

Here goes my DTO object: file ResourceGroupDTO.cs

public class ResourceGroupDTO
{
    public int ResourceGroupId
    {
        get;
        set;
    }

    public int? Position
    {
        get;
        set;
    }

    [JsonProperty("groupname")]
    [RegularExpression(Constants.GeneralStringRegularExpression)]
    public string GroupName
    {
        get;
        set;
    }
}

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    // Auto Mapper Configurations
    var mappingConfig = new MapperConfiguration(mc =>
    {
        mc.AddProfile(new MappingProfile());
    }

    );
    IMapper mapper = mappingConfig.CreateMapper();
    services.AddSingleton(mapper);
}

MappingProfile.cs

public class MappingProfile : Profile
{
    public MappingProfile()
    {
        CreateMap<ResourceGroups, ResourceGroupDTO>();
        CreateMap<List<ResourceGroups>, List<ResourceGroupDTO>>();
    }
}

file ResourceGroupService.cs

public class ResourceGroupService : IResourceGroupService
{
    private readonly DemoDbContext _dbContext;
    private readonly ICommonService _commonService;
    private readonly IMapper _mapper;
    public ResourceGroupService(DemoDbContext dbContext, ICommonService commonService, IMapper mapper)
    {
        _dbContext = dbContext ?? throw new ArgumentNullException(nameof(dbContext));
        _commonService = commonService ?? throw new ArgumentNullException(nameof(commonService));
        _mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
    }

    public async Task<ResourceGroupDTO> GetResourceGroupDetailsAsync(int resourceGroupId, int languageId)
    {
        var resourceGroup = await _dbContext.ResourceGroups.Where(rg => rg.ResourceGroupId.Equals(resourceGroupId) && rg.IsPublished.Equals(true))
                                                               .Select(rg => new { rg.ResourceGroupId, rg.Position, rg.GroupName, rg.LanguageId })
                                                               .AsNoTracking().ToListAsync();
        var resGroup = resourceGroup.FirstOrDefault(rg => rg.LanguageId.Equals(languageId));
        return _mapper.Map<ResourceGroupDTO>(resGroup);
    }
}

While debugging the above code I get the below error:

Missing type map configuration or unsupported mapping. \n \nMapping types :  \
n<> f__AnonymousType4 ` 4 ->
ResourceGroupDTO \n<> f__AnonymousType4 ` 4
[
[System.Int32, System.Private.CoreLib, Version =  4.0  .0  .0 , Culture =  neutral, PublicKeyToken =  7  cec85d7bea7798e] , 
[System.Nullable ` 1[
[System.Int32, System.Private.CoreLib, Version =  4.0  .0  .0 , Culture =  neutral, PublicKeyToken =  7  cec85d7bea7798e] ] , 
System.Private.CoreLib , Version =  4.0  .0  .0 ,  Culture =  neutral , PublicKeyToken =  7  cec85d7bea7798e ] , [System.String, System.Private.CoreLib, Version =  4.0  .0  .0 , Culture =  neutral, PublicKeyToken =  7  cec85d7bea7798e] , 
[System.Int32, System.Private.CoreLib, Version =  4.0  .0  .0 , Culture =  neutral, PublicKeyToken =  7  cec85d7bea7798e] ] ->
Author.Query.Persistence.DTO.ResourceGroupDTO \n --  ->AutoMapper.AutoMapperMappingException : Missing type map configuration or unsupported mapping. \n \nMapping types :  \
n<> f__AnonymousType4 ` 4 ->
1

There are 1 answers

0
Shoejep On BEST ANSWER

You're trying to map an anonymous type, i.e.

new { rg.ResourceGroupId, rg.Position, rg.GroupName, rg.LanguageId }

onto a ResourceGroupDTO, hence the error. To quickly fix your error you could just change the above to

new ResourceGroupDTO { ResourceGroupId = rg.ResourceGroupId, Position = rg.Position, GroupName = rg.GroupName, LanguageId = rg.LanguageId }

and then add LanguageId to ResourceGroupDTO and get rid of the mapper.

But what you should really be using is ProjectTo and you should change your .FirstOrDefault to a .Where - to make your query more efficient, in the format shown below:

await _dbContext.ResourceGroups
.AsNoTracking()
.Where(/* Put your where here */)
.ProjectTo<ResourceGroupDTO>(_mapper.Configuration)
.FirstOrDefaultAsync();

You can also simplify your startup