J.G.Sable
J.G.Sable

Reputation: 1388

Unsupported Mapping .NET 5 using AutoMapper

I have an profile mapping file.

public class AutoMapperProfiles : Profile
    {
        public AutoMapperProfiles()
        {
            #region MyFunObjects
            CreateMap<CreateMyFunObjectDto, MyFunObject>();
            CreateMap<MyFunObject, MyFunObjectDetailDto>();
            #endregion
        }
    }

MyFunObject Model is simple:

 public class MyFunObject 
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
    }

My dto's are simple:

 public class CreateMyFunObjectDto
    {
        [Required]
        public string Name { get; set; }
    }

 public class MyFunObjectDetailDto
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }

Automapper is registered in ConfigureServices of Startup.cs

services.AddAutoMapper(typeof(Startup));

And in my service, this line breaks and throws an unhandled error "Unsupported Mappings".

 MyFunObject myFunObject = _mapper.Map<CreateMyFunObjectDto, MyFunObject >(createMyFunObjectDto);

this also fails in another method call.

return _mapper.Map<List<MyFunObjectDetailDto>>(myFunObjectList);

What am I missing?

Stack trace:

blazor.server.js:19 [2021-01-30T15:26:00.306Z] Error: AutoMapper.AutoMapperMappingException: Error mapping types.

Mapping types:
Object -> List`1
System.Object -> System.Collections.Generic.List`1[[Ruak_Models.DTOs.StatusDtos.StatusDetailDto, Ruak_Models, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]
 ---> AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types:
Status -> StatusDetailDto
Ruak_Data.Models.Status -> Ruak_Models.DTOs.StatusDtos.StatusDetailDto
   at lambda_method90(Closure , Status , StatusDetailDto , ResolutionContext )
   at lambda_method89(Closure , Object , List`1 , ResolutionContext )
   --- End of inner exception stack trace ---
   at lambda_method89(Closure , Object , List`1 , ResolutionContext )
   at Ruak_Business.Services.StatusService.StatusService.GetStatuses() in C:\Code\BlazorRuak\RuakBlazor\Ruak\Ruak_Business\Services\StatusService\StatusService.cs:line 53
   at Ruak_Server.Pages.Objectives.ObjectiveList.OnInitializedAsync() in C:\Code\BlazorRuak\RuakBlazor\Ruak\Ruak_Server\Pages\Objectives\ObjectiveList.razor.cs:line 28
   at Microsoft.AspNetCore.Components.ComponentBase.RunInitAndSetParametersAsync()
   at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask(Task taskToHandle)

Upvotes: 1

Views: 2509

Answers (1)

atiyar
atiyar

Reputation: 8305

AutoMapper cannot find the maps you have defined/configured in the AutoMapperProfiles class. The most probable cause would be the profile class is in a different assembly than the one which contains the Startup class.

If that is the case, then you can create a marker interface in the assembly that contains the AutoMapperProfiles class -

public interface IMappingProfile
{
    //
}

It's sole purpose is to mark/identify the assembly that contains your maps.

Then register AutoMapper as -

services.AddAutoMapper(typeof(IMappingProfile));

EDIT :

Using the marker interface is just my personal preference, as I tend to create separate profile classes for my domain models. So, you could simply omit the interface and do the registering with your profile class type as -

services.AddAutoMapper(typeof(AutoMapperProfiles));

Upvotes: 1

Related Questions