0

I'm trying to map a DataTable to a DtoObject with AutoMapper. This works perfect only when I'm trying to add some custom mapping for a specific field it is not being mapped.

using nuget packages:

  • AutoMapper
  • AutoMapper.Data

Setup (using unity):

container.RegisterInstance<IMapper>(new AutoMapperConfiguration().Configure().CreateMapper());

public class AutoMapperBootstrap : Profile
{
    public AutoMapperBootstrap()
    {
        CreateMap<IDataReader, DtoObject>()
            .ForMember(dest => dest.SomeField, opt => opt.MapFrom(src => src.GetDateTime(0)));
    }
}


public class AutoMapperConfiguration
{
    public MapperConfiguration Configure()
    {
        var config = new MapperConfiguration(cfg =>
        {
            cfg.AddDataReaderMapping();
            cfg.AddProfile<AutoMapperBootstrap>();
        });
        return config;
    }
}

Mapping:

private IEnumerable<DtoObject> DataTableReintegratieGegevensToIEnumerable(DataTable dt)
{
    return _mapper.Map<IDataReader, IEnumerable<DtoObject>>(dt.CreateDataReader());
}

Object:

public class DtoObject
{
    public DateTime? SomeField { get; set; }
    ...
}

All fields are mapped correctly with the exception of SomeField. Any ideas?

Rik
  • 513
  • 2
  • 9
  • What is the `GetDateTimeMethod` on your `src` object. Can you show the code? – ste-fu Aug 29 '18 at 09:46
  • GetDateTime method is from system.data see https://learn.microsoft.com/en-us/dotnet/api/system.data.idatarecord.getdatetime?view=netframework-4.7.2 – Rik Aug 29 '18 at 09:54

0 Answers0