AutoMapper cannot convert enum to nullable int?

前端 未结 2 697
死守一世寂寞
死守一世寂寞 2021-01-04 22:03

I got AutoMapperMappingException exception

Exception of type \'AutoMapper.AutoMapperMappingException\' was thrown. ---> System.InvalidCastException: I

2条回答
  •  情歌与酒
    2021-01-04 22:30

    Just in case if anyone want to try using a type converter

    Mapper.CreateMap().ConvertUsing(new FooTypeConverter());
    
    public class FooTypeConverter: TypeConverter
        {
            protected override DummyTypes.Foo? ConvertCore(int? source)
            {
                return source.HasValue ? (DummyTypes.Foo?)source.Value : null;
            }
        }
    

    Cheers

提交回复
热议问题