Аз използвам:
- AutoMapper 6.1.1
- AutoMapper.Extensions.Microsoft.DependencyInjection 3.0.1
Изглежда профилите ми не се зареждат, всеки път, когато извикам mapper.map, получавам AutoMapper.AutoMapperMappingException: „Липсваща конфигурация на карта на тип или неподдържано картографиране.“
Ето моя метод ConfigureServices от клас Startup.cs
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
//register automapper
services.AddAutoMapper();
.
.
}
В друг проект, наречен xxxMappings, имам своите профили за картографиране. Примерен клас
public class StatusMappingProfile : Profile
{
public StatusMappingProfile()
{
CreateMap<Status, StatusDTO>()
.ForMember(t => t.Id, s => s.MapFrom(d => d.Id))
.ForMember(t => t.Title, s => s.MapFrom(d => d.Name))
.ForMember(t => t.Color, s => s.MapFrom(d => d.Color));
}
public override string ProfileName
{
get { return this.GetType().Name; }
}
}
И наречете картата по този начин в сервизен клас
public StatusDTO GetById(int statusId)
{
var status = statusRepository.GetById(statusId);
return mapper.Map<Status, StatusDTO>(status); //map exception here
}
status има стойности след извикване на statusRepository.GetById
За моите класове Profile, ако вместо наследяване от Profile наследя от MapperConfigurationExpression, получих единичен тест като този по-долу, който казва, че картографирането е добро.
[Fact]
public void TestStatusMapping()
{
var mappingProfile = new StatusMappingProfile();
var config = new MapperConfiguration(mappingProfile);
var mapper = new AutoMapper.Mapper(config);
(mapper as IMapper).ConfigurationProvider.AssertConfigurationIsValid();
}
Предполагам, че съпоставянията ми не се инициализират. Как мога да проверя това? Изпускам ли нещо? Видях претоварване за метода AddAutoMapper().
services.AddAutoMapper(params Assembly[] assemblies)
Трябва ли да предам всички сборки в моя проект xxxMappings. Как мога да направя това?