I am using Automapper 5.2.0 in my project. When I use ProjectTo()in code, get this error:
The cartographer is not initialized. Call Initialize with the appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure that you do not have calls to the static Mapper.Map methods, and if you use ProjectTo or UseAsDataSource extension methods, make sure you pass the appropriate IConfigurationProvider. example.
Service code
public async Task<FreelancerProfileViewModel> GetFreelancerProfile()
{
var id = Guid.Parse(_identity.GetUserId());
var model = await _freelancerProfiles
.AsNoTracking()
.Where(_ => _.User.Id == id)
.ProjectTo<FreelancerProfileViewModel>()
.FirstAsync();
return model;
}
Automapper Profile
public class FreelancerDashbordProfile : Profile
{
private readonly IIdentity _identity;
public FreelancerDashbordProfile(IIdentity identity)
{
_identity = identity;
var id = Guid.Parse(_identity.GetUserId());
CreateMap<FreelancerProfile, FreelancerProfileViewModel>()
.ForMember(_ => _.DoingProjectCount,
__ => __.MapFrom(_ => _.Projects.Count(project => project.ProjectState == ProjectState.Doing)))
.ForMember(_ => _.EndProjectCount,
__ => __.MapFrom(_ => _.Projects.Count(project => project.ProjectState == ProjectState.End)))
.ForMember(_ => _.ProjectCount, __ => __.MapFrom(_ => _.Projects.Count));
}
}
I also use StructureMap for IoC
AutoMapperRegistery
public AutoMapperRegistery()
{
this.Scan(scan =>
{
scan.TheCallingAssembly();
scan.AssemblyContainingType<SkillProfile>();
scan.WithDefaultConventions();
scan.AddAllTypesOf<Profile>().NameBy(item => item.FullName);
});
this.For<MapperConfiguration>().Singleton().Use("MapperConfig", ctx =>
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMissingTypeMaps = true;
addAllCustomAutoMapperProfiles(ctx, cfg);
});
config.AssertConfigurationIsValid();
return config;
});
this.For<IMapper>()
.Singleton()
.Use(ctx => ctx.GetInstance<MapperConfiguration>().CreateMapper(ctx.GetInstance));
}
I See Other Question and release , but did not solve my problem.