1

Say I have:

var dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("Name", typeof(string));
dt.Columns.Add("Data Guid", typeof(Guid));
dt.Rows.Add(10, "Jonesy", Guid.NewGuid());

I know I can map this to my custom class Poco using AutoMapper with:

public class Poco
{
    public int ID { get; set; }
    public string Name { get; set; }
    public Guid DataGuid { get; set; }
}  

and

var pocos = AutoMapper.Mapper.DynamicMap<IDataReader, List<Poco>>(dt.CreateDataReader());

But, my column named Data Guid isn't mapped correctly as there is a space in the name. Is there any way I can tell AutoMapper to remove spaces in column names before the mapping?

I've looked into a custom type converter and using the BeforeMap extension. However I'm unable to alter the schema. Is this possible?

Just to cover all my bases, this is for a much larger project with the data coming from a source I cannot manipulate.

Jonesopolis
  • 25,034
  • 12
  • 68
  • 112
  • Which version of automapper are you using? DataReader support has been removed from core AutoMapper recently: https://github.com/AutoMapper/AutoMapper/commit/33831e3b53ed1cd698558daa2db483906ec5c13a – Andrew Savinykh May 06 '15 at 00:10

1 Answers1

1

Define property map for your member like this:

AutoMapper.Mapper.CreateMap<IDataRecord, Poco>()
    .ForMember(x => x.DataGuid, o => o.MapFrom(
        s => s.GetGuid(s.GetOrdinal("Data Guid"))));

And then call Automapper like this:

var pocos = AutoMapper.Mapper.Map<List<Poco>>(dt.CreateDataReader());

DynamicMap is more for when you work with anonymous (dynamic) types.

Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158