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
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.
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.
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"));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With