AutoMapper TypeConverter mapping a null type to a null type

I use AutoMapper and I registered TypeConverter to display zero long values ​​for such long values:

public class NullableLongToLongConverter : TypeConverter<long?, long> { protected override long ConvertCore(long? source) { return source ?? 0; } } 

This works fine and automatically picks up any zero lengths that convert to longs.

However, I have a few more cards that want to "convert" a number from zero long to zero long. They also use this converter. For example, both properties are set to 0 in the code below, but I expect NullableLong to be null. Am I doing something wrong?

 public class Foo { public long? NullableLong{get {return null;}} ...... } public class Bar { public long NotNullableLong{get;set;} public long? NullableLong{get;set;} ...... } public static class ComplicatedMapRegister { public static void RegisterMap() { Mapper.CreateMap<Foo, Bar>() .ForMember(b => b.NotNullableLong, opt => opt.MapFrom(f.NullableLong)) .ForMember(b => b.NullableLong, opt => opt.MapFrom(f.NullableLong)) ..... } } public static class AutoMapperRegistration { public static void RegisterAllMaps() { Mapper.CreateMap<long?, long>().ConvertUsing<NullableLongToLongConverter>(); ComplicatedMapRegister.RegisterMap(); } } 
+2
source share
1 answer

Have you registered only a converter that displays from long? to long . You just do not need to create and register a “converter” that will be displayed from long? to long? ?

0
source

All Articles