天天看點

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

作者:

旭東

出處:http://www.cnblogs.com/HQFZ

關于作者:專注于微軟平台項目架構、管理和企業解決方案。現主要從事WinForm、ASP.NET、WPF、WCF、等方面的項目開發、架構、管理。如有問題或建議,請不吝指教!

本文版權歸作者,歡迎轉載,但未經作者同意必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接。如有問題,可以聯系我,非常感謝。

如果您該文覺得不錯或者對你有幫助,請點下推薦,讓更多的朋友看到,謝謝!