自动映射例外

时间:2012-08-22 05:53:37

标签: c# automapper

以下映射代码会导致automapper在完成映射时抛出异常。

mapper.CreateMap<SqlDataReader, CodeModel>()
      .ForMember(dest => dest.Code, 
                 options => options.MapFrom(src => src["code"] != null ? 
                                                   src["code"].ToString() :
                                                   string.Empty));

这真的很奇怪,src["code"]有一个值,它是"E",甚至在automapper消息中都说明了这一点。

有没有人对此问题或如何解决此问题有任何见解?

堆栈追踪:

  

[NullReferenceException:对象引用未设置为的实例   对象。] AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext   context,IMappingEngineRunner mapper)+116
  AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext   上下文)+459

     

[AutoMapperMappingException:

     

映射类型:字符串 - &gt; String System.String - &gt; System.String

     

目标路径:CodeModel.Code

     

来源价值:E]
  AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext   上下文)+537
  AutoMapper.Mappers.DataReaderMapper.MapPropertyValue(ResolutionContext   context,IMappingEngineRunner mapper,Object mappedObject,PropertyMap   propertyMap)+305
  AutoMapper.Mappers.DataReaderMapper.MapPropertyValues(ResolutionContext   context,IMappingEngineRunner mapper,Object result)+210
  AutoMapper.Mappers.DataReaderMapper.Map(ResolutionContext context,   IMappingEngineRunner mapper)+639
  AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext   上下文)+477 AutoMapper.MappingEngine.Map(对象源,类型   sourceType,Type destinationType,Action 1 opts) +176
AutoMapper.MappingEngine.Map(Object source, Action
1 opts)+162
  AutoMapper.MappingEngine.Map(对象源)+143

1 个答案:

答案 0 :(得分:0)

尝试使用ResolveUsing方法,而不是方法MapFrom,但在您的情况下,这两种方法都应该按预期工作。尝试查看是否存在任何配置问题。

  mapper.CreateMap<SqlDataReader, CodeModel>()
          .ForMember(dest => dest.Code, 
                     options => options.ResolveUsing(src => src["code"] != null ? 
                                                       src["code"].ToString() :
                                                       string.Empty));

我用SqlReaderMock创建了一个工作示例。我假设您的SqlReader执行上下文很好。

//Arrange
var configStore = new ConfigurationStore(new TypeMapFactory(), MapperRegistry.AllMappers());
configStore.CreateMap<SqlDataReaderMock, Destination>()
    .ForMember(dest => dest.Name, opt => opt.ResolveUsing(src => src["tyto"] != null
                                                                        ? src["tyto"].ToString()
                                                                        : String.Empty));
var _mappingEngine = new MappingEngine(configStore);

//Act
var result = _mappingEngine.Map<Destination>(new SqlDataReaderMock());

//Assert
Assert.AreEqual("otyt", result.Name);


class Destination
{
    public string Name { get; set; }
}


class SqlDataReaderMock
{
    public string this[string value]
    {
        get
        {
            return new string(value
                                    .ToCharArray()
                                    .Reverse()
                                    .ToArray());
        }
    }
}