Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm new to AutoMapper and have couple of questions regarding datatable to object mapping.I did some work but seems like something went wrong.

Mapper.CreateMap<IDataReader, OrderDest>().ConvertUsing<OrderDestTypeConverter>();

public class OrderDestTypeConverter : ITypeConverter<IDataReader, OrderDest>
    {
        public OrderDest Convert(ResolutionContext context)
        {
            var dest = new OrderDest();
            if (!context.IsSourceValueNull && context.SourceValue is IDataReader)
            {
                var dr = (IDataReader) context.SourceValue;
                dest.OrderQuantityDest = (int) dr["quantity"];
            }
            return dest;
        }
    }

In my repository class - I'm doing this

  var crs = new CustomerRespositorySimulator();
  DataTable orderlistsource = crs.GetCustomerOrders(12345);
  var orderlistdest = Mapper.Map<IDataReader, List<OrderDest>>(orderlistsource.CreateDataReader());

For some reason the mapping does not work . I even attached a break in custom type converter OrderDestTypeConverter class and it never gets hit.

I'm doing any wrong in using customtype converter?.

Appreciate your help!.

share|improve this question

1 Answer

up vote 0 down vote accepted

When converting from a IDataReader AutoMapper uses a special IObjectMapper internally which disregards any ITypeConverter you have applied to the mapping definition. Presumably this is so it has full control of iterating through the reader.

What you want to achieve can be done via the ForMember method when creating the map.

Mapper.CreateMap<IDataReader, OrderDest>()
    .ForMember(dest => dest.OrderQuantityDest, opt => opt.MapFrom(src => (int)src["quantity"]));
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.