question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.Map<IDataReader, FooDto> not mapping custom ForMember

See original GitHub issue

All but one of my DTO members are mapped fine, but the one with a different property name (ForMember) doesn’t get mapped.

I have a DTO class:

public class FooDto
{
   ...
   public string UserId {get; set;}
}

My database record has a string column “Id” that I want to map into FooDto, UserId. My configure includes:

CreateMap<IDataReader, FooDto>()
   .ForMember(dest => dest.UserId, opt => opt.MapFrom(src => src.GetString(src.GetOrdinal("Id"))));

When I call:

var records = mapper.Map<IDataReader, IEnumerable<FooDto>>(reader);

The records include the value for all properties except the “Id” column. In fact, I can do this and my records are still missing a value:

CreateMap<IDataReader, FooDto>()
   .ForMember(dest => dest.UserId, opt => opt.MapFrom(src => "abc")));

I don’t think the ForMember code is being executed at all during the IDataReader mapping. Any ideas?

Using AutoMapper 7.0.1 and AutoMapper.Data 2.0.0.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:21 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
ColonelDudditscommented, Sep 14, 2018

I know the root cause now. I’ll submit a PR later to fix it. In the meantime you can add AddMemberConfiguration().AddMember<DataRecordMemberConfiguration>(); to your Profile constructor and things should start mapping as expected.

1reaction
AleAuthenticGamingcommented, Feb 4, 2019

I’ve bypassed this issue by adding a mapping on IDataRecord.

 cfg.CreateMap<IDataRecord, Language>()
                                .ForMember(dest => dest.TwoLetterISOCode, opt => opt.MapFrom(src => (string)src["two_letter_iso"]))
                                .ForMember(dest => dest.ThreeLetterISOCode, opt => opt.MapFrom(src => (string)src["three_letter_iso"]));

                            cfg.CreateMap<IDataReader, Language>();

I’ve created the projection for the properties with a different name from the db field and it works correctly now

Read more comments on GitHub >

github_iconTop Results From Across the Web

Auto mapper mapping of nested object within a collection
I have a class(Question) which contains a nested propertry called "PostedBy" which is a class called "User" and I am trying to map...
Read more >
Mapping From IDataReader/IDataRecord with AutoMapper
Now we can use AutoMapper to map the results to instances of our view class: var dataReader = ... // Execute a data...
Read more >
Retrieving Data Using a DataReader - ADO.NET
Learn how to retrieve data using a DataReader in ADO.NET with this sample code. DataReader provides an unbuffered stream of data.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found