.Net Core Automapper missing type map configuration or unsupported mapping

Viewed 18992

Net core application. I am trying to use Auto mapper but results in the below error.

.Net Core Automapper missing type map configuration or unsupported mapping

I have below setup in startup.cs

var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new MappingProfile());
            });

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

Then I am using profiles.

  public class MappingProfile : Profile
    {

        public MappingProfile()
        {
            this.CreateMap<Geography, GeographyEntity>();
            this.CreateMap<Model1, Model2>();
        }

    }

I am using auto mapper as below

 Model1 model = this.Mapper.Map<Model1>(Model2);

Below are the models

 public partial class Model1
    {
        public int SNo { get; set; }
        public string SarNo { get; set; }
        public string SiteName { get; set; }
        public string Client { get; set; }
        public int CId { get; set; }
        public DateTime StartDate { get; set; }
        public bool IsActive { get; set; }

        public virtual Model2 C { get; set; }
    }

public class Model2
{
    public int SNo { get; set; }

    public string SarNo { get; set; }

    public string SiteName { get; set; }

    public int CId { get; set; }

    public string Client { get; set; }

    public bool? IsActive { get; set; }

    public DateTime StartDate { get; set; }

}

I get below error in auto mapper.

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

can someone help me to understand this error? Any help would be greatly appreciated. Thanks

4 Answers

this.CreateMap<Model1, Model2>(); will create map from Model1 to Model2, so this should work:

Model2 model = this.Mapper.Map<Model2>(new Model1());

If you want it vise versa either change the registration to:

this.CreateMap<Model2, Model1>(); 

or add ReverseMap to have bidirectional one:

this.CreateMap<Model1, Model2>().ReverseMap();

I had the same error. I spent hours so I thought I would mention it here.

I had different projects for Api, domains/entities, DTOs, and mapping profiles.

There were two issues. First, I forgot to mark the DTO mapping profiles classes as public. Second, I had the registered automapper in startup class as below:

services.AddAutoMapper(AppDomain.CurrentDomain.GetAssemblies());

The above line works fine if your mapping profiles are in the same assembly. In my case mapping profiles were in a different assembly, and therefore this was not picking up the profiles. To resolve this, I used the following:

services.AddAutoMapper(Assembly.Load("Your assembly name"));

In some tutorials instead of

var mappingConfig = new MapperConfiguration(mc =>
        {
            mc.AddProfile(new MappingProfile());
        });

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

using

service.AddAutoMapper({profile assembly marker types});

Usually, in those tutorials, they were referring to startup. In practical that assembly marker should be your profile mapping class. In this case, it should be

service.AddAutoMapper(typeof(MappingProfile));

In net core 6 I solved the problem with the @Amit recomendation above.

It is, In Proram.cs I had: builder.Services.AddAutoMapper(typeof(Program));

as automapper page recommended and I was receiving the missing mapping exception.

I changed to the following and solve the problem:

builder.Services.AddAutoMapper(typeof(AutoMapperProfile));
Related