Search code examples
c#.net-coreautomapper

.NET Core AutoMapper missing type map configuration or unsupported mapping


.NET Core application. I am trying to use Automapper 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


Solution

  • 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();