2017-01-31 31 views
3

我使用Automapper 5.2.0在我的项目。当我使用ProjectTo()在代码中出现此错误:映射器未初始化,当使用ProjectTo()

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

Automapper档案

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

    } 

} 

而且我使用StructureMapIoC

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


    } 

我看到其他QuestionIssue但不解决我的问题。

回答

9

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

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; 
} 
+0

是否有任何可能的方式将配置提供程序传递给所有映射?我认为这不是一个好的解决方案。我必须将配置文件传递给我的所有查询吗?我没有找到任何更好的解决方案:/ – ahmet

相关问题