Reputation: 205
I'm new to AutoMapper, so this is probably a beginner's question. I've searched, but haven't seen this discussed. When creating a map, what is the difference between the ForMember and ForSourceMember methods:
Mapper.CreateMap<Role, RoleDto>()
.ForMember(x => x.Users, opt => opt.Ignore())
.ForSourceMember(x => x.Users, opt => opt.Ignore());
I'm maintaining code written by others. In some places, I see ForMember, in others ForSourceMember, and as shown above, in one place both.
What's the difference between the two?
Thanks in advance for any assistance.
Upvotes: 16
Views: 12383
Reputation: 109255
Look at the method signatures. In ...
Mapper.CreateMap<Role, RoleDto>()
.ForMember(x => x.Users, opt => opt.Ignore())
.ForSourceMember(x => x.Users, opt => opt.Ignore());
... ForMember
is a method that expects an Expression<Func<RoleDto>>
parameter named destinationMember
, whereas ForSourceMember
expects an Expression<Func<Role>>
parameter named sourceMember
. So
ForMember
configures members of the target type.ForSourceMember
configures members of the source type.In your case, both the source and the target types have members UserId
, so the calls look the same, but they aren't. They should do the same thing, but the funny thing is that ForSourceMember
doesn't seem to have any effect in ignoring members. Maybe it's a bug.
Upvotes: 16