3
votes

The following mapping code causes automapper to throw an exception when the map is done.

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

This is really bizarre, src["code"] has a value, it is "E", this is even stated in the automapper message.

Does anyone have any insight into what this issue or how to solve this issue?

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.] AutoMapper.Mappers.TypeMapMapper.Map(ResolutionContext context, IMappingEngineRunner mapper) +116
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +459

[AutoMapperMappingException:

Mapping types: String -> String System.String -> System.String

Destination path: CodeModel.Code

Source value: E]
AutoMapper.MappingEngine.AutoMapper.IMappingEngineRunner.Map(ResolutionContext context) +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 context) +477 AutoMapper.MappingEngine.Map(Object source, Type sourceType, Type destinationType, Action1 opts) +176
AutoMapper.MappingEngine.Map(Object source, Action
1 opts) +162
AutoMapper.MappingEngine.Map(Object source) +143

1

1 Answers

0
votes

Try to use ResolveUsing method, instead of the method MapFrom, but in your case both these methods should work as expected. Try to see if any configuration issues are present.

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

I have created a working sample with SqlReaderMock. I assume your context of SqlReader execution is good.

//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());
        }
    }
}