AutoMapper使用中的问题

指定值只会执行一次

 

  public class MomanBaseProfile : Profile
    {
        public MomanBaseProfile()
        {
            CreateMap<RequestBase, MomanEntity>()
                .ForMember(d => d.ID, op => op.Ignore())
                .ForMember(d => d.UID, op => op.UseValue(Guid.NewGuid()))//使用Mapper过程中始终不变
                .ForMember(d => d.CreateTime, op => op.UseValue(DateTime.UtcNow))
                .ForMember(d => d.UpdateTime, op => op.UseValue(DateTime.UtcNow));

            CreateMap<RequestWithUserBase, MomanEntity>()
                .IncludeBase<RequestBase, MomanEntity>();
        }
    }

 

继承类如何减少Map问题解决

 
public class MomanBaseProfile : Profile
    {
        public MomanBaseProfile()
        {
            CreateMap<RequestBase, MomanEntity>()
                .ForMember(d => d.ID, op => op.Ignore())
                .ForMember(d => d.UID, x => x.MapFrom(ax => Guid.NewGuid()))
                .ForMember(d => d.CreateTime, x => x.MapFrom(ax => DateTime.UtcNow))
                .ForMember(d => d.UpdateTime, x => x.MapFrom(ax => DateTime.UtcNow));

           //继承类两种方式,一种使用IncludeBase(从父类映射继承)如下,另一中使用Include(从子类映射继承)
            CreateMap<RequestWithUserBase, MomanEntity>()
                .IncludeBase<RequestBase, MomanEntity>();
        }
    }
 

参考

AutoMapper: Why is UseValue only executed once

posted @ 2016-09-26 16:06  霍旭东  阅读(1441)  评论(1编辑  收藏  举报