问题
We currently have a viewmodel (custom c# class
) which has two properties is_active
and is_deleted
which are bool. the entity class which is saved/retrieved by the db calls has those fields as string
, as in the DB they are stored to 'Y' and 'N'.
Currently we have this for the AutoMapper mapping:
CreateMap<classOne, classTwo>()
.ForPath(d => d.IS_ACTIVE, opt => opt.MapFrom(s => s.IS_ACTIVE == "Y" ? true : false))
.ForPath(d => d.IS_DELETED, opt => opt.MapFrom(s => s.IS_DELETED == "Y" ? true : false));
CreateMap<classTwo, classOne>()
.ForPath(d => d.IS_ACTIVE, opt => opt.MapFrom(s => s.IS_ACTIVE ? "Y" : "N"))
.ForPath(d => d.IS_DELETED, opt => opt.MapFrom(s => s.IS_DELETED ? "Y" : "N"));
This works, but is there a cleaner way to do this? so going from classOne to classTwo and reverse work? We will have many of these so trying to find a cleaner approach.
来源:https://stackoverflow.com/questions/55616296/automapper-true-false-to-y-n-and-reverse