'MapFrom not working properly in AutoMapper 11.0.1

Upgrading to AutoMapper v11.0.1 from v10.1.1 causes any profiles I have that do a .ForMember mapping to ignore those members. I know the Profile is being read as it maps the rest of the object properly. The project also coincidentally was upgraded to .Net 6.0 from .Net Core 3.1, but based on my testing and downgrading between various versions, it appears AutoMapper is the culprit.

The profile and objects are as follows:

// SeminarEventProfile 
public class SeminarEventProfile : Profile
{
    public SeminarEventProfile()
    {
        CreateMap<ViewModels.SeminarEvent, UpdateSeminarEvent>()
            .ForMember(
                dest => dest.StartDate,
                opt => opt.MapFrom(src => Convert.ToDateTime(src.Date + ' ' + src.StartTime))
            )
            .ForMember(
                dest => dest.EndDate,
                opt => opt.MapFrom(src => Convert.ToDateTime(src.Date + ' ' + src.EndTime))
            )
            .ForMember(
                dest => dest.AbstractProperty,
                opt => opt.MapFrom(src => src.Abstract)
            );

    }
}

// relevant fields from ViewModels.SeminarEvent
public class SeminarEvent
{
    public string StartTime { get; set; }

    public string EndTime { get; set; }

    public string Abstract { get; set; } = string.Empty;
}

// relevant fields from UpdateSeminarEvent - generated AutoRest model
public partial class UpdateSeminarEvent
{
    public System.DateTime? StartDate { get; set; }

    public System.DateTime? EndDate { get; set; }

    public string AbstractProperty { get; set; }
}

The following is the debugger of the incoming object:

View of a debugger of ViewModels.SeminarEvent data fields

In AutoMapper v10.1.1, it properly maps the AbstractProperty, EndTime, and StartTime.

enter image description here

In AutoMapper v11.0.1, all values are null.

enter image description here



Solution 1:[1]

We have the exact same error: AutoMapper's behavior regarding constructors seems to have changed in version 11.

AutoRest generates classes that have the following constructors. As soon as these constrcutors are there (it doesnt matter if the code was generated from AutoRest), the AutoMapper only returns empty objects and ignores the whole configuration for this Type.

public ctor(string field1 = null, string field2 = null)
{
   this.Field1 = field1;
   this.Field2 = field2
}

Edit: The mapping only fails with the generic method _mapper.Map<Type>(source). The mapping via _mapper.Map(source, destination) works as expected.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stephan