Combining ConvertUsing and IncludeMembers #3985
-
I'm having an issue combining void Main()
{
var cfg = new AutoMapper.MapperConfiguration(cfg =>
{
cfg.CreateMap<B, Dto>()
.ForMember(d => d.DT, m => m.ConvertUsing(UtcDateConverter.ToNullableDateTimeOffset, b => b.DT));
cfg.CreateMap<A, Dto>()
.IncludeMembers(a => a.B);
});
cfg.AssertConfigurationIsValid();
var mapper = cfg.CreateMapper();
mapper.ProjectTo<Dto>(new [] { new A() }.AsQueryable());
}
// The implementation of UtcDateConverter is not relevant here
class B { public DateTime DT { get; set; } }
class A { public B B { get; set; } }
class Dto { public DateTimeOffset? DT { get; set; } } Running this gives
using cfg.CreateMap<A, Dto>()
.ForMember(d => d.DT, m => m.ConvertUsing(UtcDateConverter.ToNullableDateTimeOffset, a => a.B.DT)); brings back the slight different message:
and the only solution is to map it as:
However, as I reuse this logic in several places (but places I need to be explicit about), I'd really rather use a converter that I can wrap in an extension method. 🤔 Curiously, I get different errors when I use c#8 and |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
This is the expected behavior. You don't get null checks as you do with |
Beta Was this translation helpful? Give feedback.
This is the expected behavior. You don't get null checks as you do with
MapFrom
. But it seems to me that the right mapping here is with a type converter, not a value converter.