AutoMapper
ForMember
C# programming
object mapping
software development

How to use AutoMapper .ForMember?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

ForMember is the AutoMapper hook you use when convention-based mapping is not enough for a specific destination property. It lets you say exactly how one destination member should be populated, ignored, transformed, or conditionally mapped.

What ForMember Configures

AutoMapper starts with conventions: properties with matching names and compatible types are copied automatically. ForMember is for the exceptions.

You use it on a map definition like this:

csharp
cfg.CreateMap<Source, Destination>()
   .ForMember(dest => dest.SomeProperty, opt => /* custom rule */);

The first lambda points to the destination member. The second lambda configures how AutoMapper should fill it.

Common ForMember Operations

The most useful patterns are:

  • 'MapFrom for custom source expressions'
  • 'Ignore when a destination property should not be mapped'
  • 'Condition or PreCondition for selective mapping'
  • 'NullSubstitute for null fallback values'
  • 'ConvertUsing when a member needs a value converter'

Here is a practical example:

csharp
1using AutoMapper;
2using System;
3
4public class User
5{
6    public string FirstName { get; set; } = "";
7    public string LastName { get; set; } = "";
8    public DateTime CreatedAt { get; set; }
9    public string? Nickname { get; set; }
10}
11
12public class UserDto
13{
14    public string FullName { get; set; } = "";
15    public string CreatedDate { get; set; } = "";
16    public string DisplayName { get; set; } = "";
17}
18
19public class UserProfile : Profile
20{
21    public UserProfile()
22    {
23        CreateMap<User, UserDto>()
24            .ForMember(dest => dest.FullName,
25                opt => opt.MapFrom(src => src.FirstName + " " + src.LastName))
26            .ForMember(dest => dest.CreatedDate,
27                opt => opt.MapFrom(src => src.CreatedAt.ToString("yyyy-MM-dd")))
28            .ForMember(dest => dest.DisplayName,
29                opt => opt.NullSubstitute("anonymous"));
30    }
31}

This is the most common use of ForMember: a destination type that does not match the source one-to-one.

MapFrom, Ignore, and Conditions

MapFrom is the core operation. It answers, "Where should this destination member get its value?"

Ignoring is equally important when a destination field is computed elsewhere:

csharp
CreateMap<User, UserDtoWithIgnoredField>()
    .ForMember(dest => dest.ServerCalculatedValue, opt => opt.Ignore());

Conditional mapping is useful when source data is incomplete or expensive to compute:

csharp
1CreateMap<User, UserDto>()
2    .ForMember(dest => dest.DisplayName, opt =>
3    {
4        opt.PreCondition(src => !string.IsNullOrWhiteSpace(src.Nickname));
5        opt.MapFrom(src => src.Nickname);
6    });

PreCondition runs before AutoMapper resolves the source value. Condition runs later. That distinction matters when the mapping expression is expensive or fragile.

ForMember and Nested Data

You can flatten nested source structures with MapFrom:

csharp
CreateMap<Order, OrderDto>()
    .ForMember(dest => dest.CustomerName,
        opt => opt.MapFrom(src => src.Customer.Name));

If you need to configure a nested destination path instead of a top-level destination member, use ForPath, not ForMember.

That is a common source of confusion:

  • 'ForMember: top-level destination property'
  • 'ForPath: nested destination path'

Keep Mapping Rules Small

AutoMapper profiles are best when they describe mapping, not business workflows. If a mapping needs database access, service calls, or complex branching, that logic usually belongs outside the profile.

A good rule is:

  • shape changes and simple formatting belong in ForMember
  • business decisions belong in application code

That separation keeps the mapping configuration readable and testable.

ProjectTo Versus In-Memory Mapping

Be careful if you use ProjectTo with LINQ providers such as Entity Framework. Not every ForMember configuration can be translated to SQL.

Simple MapFrom expressions often work well. Arbitrary .NET method calls, custom resolvers, or some converters may not translate. If the map is for database projection, keep the expression translation-friendly.

Common Pitfalls

The biggest mistake is writing the lambda against the source member instead of the destination member. In ForMember, the first lambda must point to the destination property.

Another common mistake is using ForMember for nested destination paths. That is a ForPath scenario.

People also put too much business logic into mapping profiles. If a profile starts looking like a service layer, the design is drifting.

Finally, remember that some member-level conversions work only for in-memory mapping and not for query translation with ProjectTo.

Summary

  • Use ForMember when AutoMapper conventions are not enough for one destination property.
  • 'MapFrom, Ignore, NullSubstitute, and conditions are the most common options.'
  • The configured lambda targets a destination member, not a source member.
  • Use ForPath when the destination member is nested.
  • Keep mapping profiles focused on shape conversion, not full business logic.

Course illustration
Course illustration

All Rights Reserved.