'Why is Automapper or automapper.data behaving differently in .NET framework and .NET 6?

I have a Web API that was written in .NET 4.6.1 and I want to move it to .NET 6.

I use Automapper.Data to map SQL data reader to a C# object and found a strange behaviour.

Say this is the output object:

public class PriceInfoV11
{
    public List<Price> Prices { get; set; } = new List<Price>();
}    

public class ThunderPriceInfoV11 : PriceInfoV11
{
    [JsonIgnore]
    public double? BidRaw { get; set; }
}

We can see that ThunderPriceInfoV11 inherits from PriceInfoV11 and has two properties, BidRaw and a list of Price objects.

When AutoMapper was running in .NET 4.6 (or 4.8), the following config works when mapping an IDataReader to ThunderPriceInfoV11:

var config = new MapperConfiguration(cfg =>
            {
                cfg.AddDataReaderMapping();
                cfg.CreateMap<IDataReader, ThunderPriceInfoV11>()
                    .ForMember(fu => fu.BidRaw,
                        u => u.MapFrom(um =>
                            um.IsDBNull(um.GetOrdinal("Bid")) ? (double?)null : (double)um.GetDecimal(um.GetOrdinal("Bid"))))
                    ;
            });
_mapper = config.CreateMapper(); 

However, in .NET 6, this same config throws an error saying it doesn't know property Prices.

To make it work, I need to add:

.ForMember(fu => fu.Prices, opt => opt.Ignore())

I am bit confused on why the same version of AutoMapper and AutoMapper.Data behaves differently in .NET 4.8 and .NET 6. I tried the latest version of AutoMapper and an older version, and this behaviour is the same in both.



Sources

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

Source: Stack Overflow

Solution Source