Im trying to use automapper to map from one record to a new record and add a few defaults based on some logic. I was originally expecting "ForCtorParam" to work, but im still getting the same exception after adding it.
public record Source(
int number);
public record Destination(
int number,
string numberAsString);
[Fact]
public void automapperTest()
{
var config = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Source, Destination>()
.ForCtorParam("numberAsString", (opt) => opt.MapFrom((src) => src.number.ToString()));
});
config.AssertConfigurationIsValid();
}
Im not sure if i am trying to do something that is not intended or missunderstanding the automapper documentation.
Hoping someone in here can help TIA
Your code is going to work when mapping one record to another. However, the
AssertConfigurationIsValidmethod is going to throw an errorUnmapped properties: number, numberAsString.For me, it helped to add the
.ForAllMembers(opt => opt.Ignore())at the end of the mapping. This will resolve the validation issue and the mapping is going to work as normal.You can check that with a unit-test mapping the two records.