簡體   English   中英

當我使用 AutoMapper 時,如果這些屬性具有相同的名稱,我是否需要明確地編寫所有屬性?

[英]Do I need to write all the properties explicitly when I am using AutoMapper, if these properties have the same name?

我一直在嘗試這樣做,但是當我調試測試時,對象返回 null。 我想做復雜的對象到對象映射,但我無法讓它工作。

代替:

cfg.CreateMap<Payments, Customer.Payments>()
    .ForMember(to => to.SomeName, opts => opts.MapFrom(from => from.SomeName))
    .ForMember(to => to.SomeDate, opts => opts.MapFrom(from => from.SomeDate));

我們想做:

    cfg.CreateMap<Payments, Customer.Payments>();

如果您還沒有,我肯定會查看他們的Wiki

根據您上面的評論,您似乎對mapper.map的簽名感到困惑。

這是你可以做的: var dest = mapper.Map<Dest>(new Source());

根據您發布的代碼查看這個簡單的小提琴以獲得一個工作示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM