gpt4 book ai didi

c# - Automapper继承——重用 map

转载 作者:太空狗 更新时间:2023-10-29 19:39:55 27 4
gpt4 key购买 nike

我正在尝试使用 automapper 为父对象创建单个映射并在其子对象中重复使用它。

对于子属性,我只想映射额外的字段。

这可能吗?我的代码看起来像这样

CreateCalculationMap(message);  ---- This does the BASE parent mappping.
Mapper.CreateMap<QuoteMessage, CalculationGipMessage>() -- This is the child.
.Include<QuoteMessage, CalculationBase>() -- Include the parent?
.ForMember(a => a.OngoingCommission, b => b.MapFrom(c => c.OngoingASF)) - Child
.ForMember(a => a.SpecialRate, b => b.MapFrom(c => c.BlahBlah))); - Child

为什么它一直告诉我父属性没有映射?然而,我认为我将它们包含在 CreateCalculationMap(message);其中包含

Mapper.CreateMap<QuoteMessage, CalculationBase>() ()

最佳答案

仅供引用,我想通了

 public static IMappingExpression<A, T> ApplyBaseQuoteMapping<A, T>(this   IMappingExpression<A, T> iMappingExpression)
where A : QuoteMessage
where T : CalculationGipMessage
{
iMappingExpression
.ForMember(a => a.LoginUserName, b=> b.MapFrom(c => c.LoginUserName))
.ForMember(a => a.AssetTestExempt, b => b.Ignore())
;

return iMappingExpression;
}


Mapper.CreateMap<QuoteMessage, CalculationGipMessageChild>()
.ApplyBaseQuoteMappingToOldCol()
// do other mappings here

关于c# - Automapper继承——重用 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6212516/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com