How to handle custom Properties in AutoMapper

前端 未结 3 1513
被撕碎了的回忆
被撕碎了的回忆 2020-12-14 08:05

I\'ve got a ViewModel that takes some Model data and slightly alters it.

The way I\'m doing it \"works\" since I just pass the DomainModel to the constr

3条回答
  •  太阳男子
    2020-12-14 08:48

    Custom mapping can be defined in global.ascx (at startup) by following codes :

          AutoMapper.Mapper.CreateMap()
              .ForMember(o => o.Email, b => b.MapFrom(z => z.Email))
              .ForMember(o => o.UserName , b => b.MapFrom(user => (user.UserName != null) ? user.UserName : "User" + user.ID.ToString));
    

    you can do some initialization via BeforeMap () method. But you may need to do some changes in your viewmodel.

提交回复
热议问题