Cannot map from nullable type using ForCtorParam #4070
-
Hello, I am running into an issue when trying to map from a // source object
public sealed class Offer {
public Offer(decimal? minTransactionAmount) {
MinTransactionAmount = minTransactionAmount;
}
public decimal? MinTransactionAmount { get; }
}
// destination
public record CatalogDetails(CatalogAmount? MinTransactionAmount);
public record CatalogAmount([property: JsonConverter(typeof(DecimalToStringJsonConverter))] decimal Amount, Currencies Currency);
public enum Currencies : byte {
Usd = 0,
}
// mapping logic
CreateMap<Offer, CatalogDetails>()
.ForCtorParam(nameof(CatalogDetails.MinTransactionAmount), opt => opt.MapFrom(src => getCatalogAmount(src.MinTransactionAmount)));
private static CatalogAmount? getCatalogAmount(decimal? amount) =>
amount.HasValue ? new CatalogAmount(amount.Value, Currencies.Usd) : null; As you can see, all I want to do is simply map
All I can determine is that there is some issue with mapping from a nullable type using Any ideas? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments
-
I ended up getting this to work if I change the code to this: public record CatalogDetails(CatalogAmount? MinTransactionAmountzzz); //with zzz at the end
CreateMap<Offer, CatalogDetails>()
.ForCtorParam(nameof(CatalogDetails.MinTransactionAmountzzz), opt => opt.MapFrom(src => getCatalogAmount(src.MinTransactionAmount))); Its like I am manually setting up the mapping, but AutoMapper is still trying to automap from |
Beta Was this translation helpful? Give feedback.
-
https://docs.automapper.org/en/latest/Queryable-Extensions.html#supported-mapping-options |
Beta Was this translation helpful? Give feedback.
https://docs.automapper.org/en/latest/Queryable-Extensions.html#supported-mapping-options
https://docs.automapper.org/en/latest/11.0-Upgrade-Guide.html#createprojection