Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core Automapper missing type map configuration or unsupported mapping

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

like image 296
Niranjan godbole Avatar asked May 29 '20 10:05

Niranjan godbole


People also ask

What is AutoMapper in .NET core?

What is AutoMapper? AutoMapper is a simple library that helps us to transform one object type into another. It is a convention-based object-to-object mapper that requires very little configuration. The object-to-object mapping works by transforming an input object of one type into an output object of a different type.

How does AutoMapper work in C#?

AutoMapper in C# is a library used to map data from one object to another. It acts as a mapper between two objects and transforms one object type into another. It converts the input object of one type to the output object of another type until the latter type follows or maintains the conventions of AutoMapper.


2 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();
like image 82
Guru Stron Avatar answered Nov 04 '22 05:11

Guru Stron


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"));
like image 38
Amit Avatar answered Nov 04 '22 03:11

Amit