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 oncehtml

相關文章
相關標籤/搜索