AutoMapper使用中的问题

指定值只会执行一次

 

  public class MomanBaseProfile : Profile
    {
        public MomanBaseProfile()
        {
            CreateMap()
                .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()
                .IncludeBase();
        }
    }

 

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

 
public class MomanBaseProfile : Profile
    {
        public MomanBaseProfile()
        {
            CreateMap()
                .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()
                .IncludeBase();
        }
    }
 

参考

AutoMapper: Why is UseValue only executed once

你可能感兴趣的:(AutoMapper使用中的问题)