使用ProjectTo()时未初始化映射器 [英] Mapper not initialized, When Use ProjectTo()

查看:179
本文介绍了使用ProjectTo()时未初始化映射器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用 Automapper 5.2.0 .当我在代码中使用ProjectTo()时出现此错误:

I Use Automapper 5.2.0 In My Project. When I Use ProjectTo() In Code Get This Error:

映射器未初始化.调用使用适当的配置初始化".如果您尝试通过容器或其他方式使用mapper实例,请确保您没有对静态Mapper.Map方法的任何调用,并且如果您使用的是ProjectTo或UseAsDataSource扩展方法,请确保传递适当的IConfigurationProvider实例.

Mapper not initialized. Call Initialize with Appropriate configuration. If you are trying to use mapper instances through a container or otherwise, make sure you do not have any calls to the static Mapper.Map methods, and if you're using ProjectTo or UseAsDataSource extension methods, make sure you pass in the appropriate IConfigurationProvider instance.

服务代码

 public async Task<FreelancerProfileViewModel> GetFreelancerProfile()
    {
        var id = Guid.Parse(_identity.GetUserId());
        var model = await _freelancerProfiles
            .AsNoTracking()
            .Where(_ => _.User.Id == id)
            .ProjectTo<FreelancerProfileViewModel>()
            .FirstAsync();

     //  var viewmodel =  _mapper.Map<FreelancerProfileViewModel>(model);

        return model;
    }

自动映射器配置文件

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

    }

}

我也将 StructureMap 用于 AutoMapperRegistery

   public AutoMapperRegistery()
    {

        this.Scan(scan =>
        {
            scan.TheCallingAssembly();
            scan.AssemblyContainingType<SkillProfile>(); // for other asms, if any.
            scan.WithDefaultConventions();

            scan.AddAllTypesOf<Profile>().NameBy(item => item.FullName);
        });

        this.For<MapperConfiguration>().Singleton().Use("MapperConfig", ctx =>
        {
            var config = new MapperConfiguration(cfg =>
            {
                cfg.CreateMissingTypeMaps = true; // It will connect `Person` & `PersonViewModel` automatically.
                addAllCustomAutoMapperProfiles(ctx, cfg);
            });
            config.AssertConfigurationIsValid();

            return config;
        });

        this.For<IMapper>()
            .Singleton()
            .Use(ctx => ctx.GetInstance<MapperConfiguration>().CreateMapper(ctx.GetInstance));


    }

我看到其他问题

I See Other Question and Issue but not solved my problem.

推荐答案

您需要将MappingConfiguration提供程序传递给ProjectTo调用.

You need to pass the MappingConfiguration provider to the ProjectTo call.

public async Task<FreelancerProfileViewModel> GetFreelancerProfile()
{
    var id = Guid.Parse(_identity.GetUserId());
    var model = await _freelancerProfiles
        .AsNoTracking()
        .Where(_ => _.User.Id == id)
        .ProjectTo<FreelancerProfileViewModel>(_mapper.Configuration)
        .FirstAsync();

 //  var viewmodel =  _mapper.Map<FreelancerProfileViewModel>(model);

    return model;
}

这篇关于使用ProjectTo()时未初始化映射器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆