Home > Net >  AutoMapper - Missing type map configuration System.Object
AutoMapper - Missing type map configuration System.Object

Time:09-26

I have set up AutoMapper in EF Core in Startup.cs like so:

    services.AddAutoMapper(
        typeof(EventItemEstablishmentProfile), 
        typeof(EventItemProfile),
        typeof(GroceryItemEstablishmentProfile), 
        typeof(GroceryItemProfile),
        typeof(GroceryStoreItemEstablishmentProfile), 
        typeof(GroceryStoreItemProfile),
        typeof(RestaurantItemEstablishmentProfile), 
        typeof(RestaurantItemProfile),
        typeof(MenuItemEstablishmentProfile), 
        typeof(MenuItemProfile));

I have these AutoMapper profiles:

namespace Vepo.Domain
{
    public class GroceryItemProfile : Profile
    {
        public GroceryItemProfile()
        {
            CreateMap<GroceryItem, GroceryItemDto>();
            CreateMap<GroceryItemDto, GroceryItem>();
        }
    }
}

namespace Vepo.Domain
{
    public class GroceryItemEstablishmentProfile : Profile
    {
        public GroceryItemEstablishmentProfile()
        {
            CreateMap<GroceryItemEstablishment, GroceryItemEstablishmentDto>();
            CreateMap<GroceryItemEstablishmentDto, GroceryItemEstablishment>();
        }
    }
}

A GroceryItemEstablishment contains a field of type GroceryItem called VeganItem so we are talking about a nested mapping here.

When it comes time to using it in code like so:

public async override Task<TVeganItemEstablishmentDto> Insert(TVeganItemEstablishmentDto entity)
{
    var toReturnVeganItem = entity.VeganItem;
    var toReturnEstablishment = entity.Establishment;
    var x = mapper.Map<TVeganItem>(entity);

I get the error:

AutoMapper.AutoMapperMappingException: Missing type map configuration or unsupported mapping.

Mapping types: Object -> GroceryItem System.Object -> Vepo.Domain.GroceryItem

Why does the error say that type Object is involved at all?

Debugging the code we can see the type is not Object:

enter image description here

CodePudding user response:

You're trying to map TVeganItemEstablishmentDto to TVeganItem. I can't tell from the code itself, but I would guess that this equates to mapping GroceryItemEstablishmentDto to GroceryItem. That mapping doesn't exist in your profiles. Do you mean to instead map the entity.VeganItem property as the input?

  • Related