Reputation: 199
Whenever I'm trying to call mapper, I get this error :
Automapper missing type map configuration or unsupported mapping
I added automapper in startup class(Program.cs)
services.SetupControllers();
services.SetupSwagger();
services.SetupCache(configuration);
services.SetupAuthentication(configuration);
services.SetupAuthorization();
services.SetupAutoMapper();
services.SetupBusiness<IPersonManager, PersonManager>(); //Using automapper in PersonManager
services.SetupRepositories();
services.SetupDatabase<myDbContext>(configuration, "LDS");
AutoMapperExtension
public static class AutoMapperExtensions
{
public static IServiceCollection SetupAutoMapper(this IServiceCollection services, Assembly? assembly = null) =>
services.AddAutoMapper(assembly ?? Assembly.GetCallingAssembly());
}
MappingProfileClass
public class ScoreMappingProfile: Profile
{
public ScoreMappingProfile()
{
CreateMap<PersonScoreModel, PersonScoreEntity>();
}
}
Code that causes error in PersonManager.
private List<PersonScoreEntity> MapModelToEntity(List<PersonScoreModel> personModels)
{
List<PersonScoreEntity> personEntities = new List<PersonScoreEntity>();
foreach (var personModel in personModels)
{
var mapped = _mapper.Map<PersonScoreEntity>(personModel); //ERROR HAPPENS HERE
personEntities.Add(mapped);
}
return personEntities;
}
PersonScoreEntity
public class PersonScoreEntity: EntityBase
{
public bool IsPreferred { get; set; }
public Guid PopulationId { get; set; }
public Guid PersonId { get; set; }
public PopulationEntity Population { get; set; } = null!;
public UserEntity User { get; set; } = null!;
}
PersonModel
public class PersonScoreModel
{
public Guid Id { get; set; }
public Guid PopulationId { get; set; }
public Guid PersonId { get; set; }
public bool IsPreferred { get; set; }
}
Similar question are present on stackoverflow, I couldn't find solution that would fit my problem
Upvotes: 3
Views: 1068
Reputation: 49
Firs of all, you need to say which profile you need to map:
services.AddAutoMapper(typeof(YourProfile);
In your profile, you need to specify which object is converted to which object and vice versa:
public StudentProfile()
{
CreateMap<PersonScoreModel, PersonScoreEntity>();
CreateMap<PersonScoreEntity, PersonScoreModel>();
}
and your entities must have the same parameters as your model. Here is a link which helps me when i have simmilar task: Getting Started Guide
Upvotes: 2