c# - 带有“new”修饰符的 AutoMapper 继承
问题描述
我正在将我的项目从 AutoMapper 版本 4.2.1 更新到 6,在新版本中,我得到了一个意外的行为。
课程
class Opportunity : OpportunityLite
{
}
class OpportunityLite
{
public DealerOpportunityLite DealerOpportunity { get; set; }
}
class DealerOpportunity : DealerOpportunityLite
{
public new string BranchCode { get; set; }
}
class DealerOpportunityLite
{
public string BranchCode { get; set; }
}
映射器配置
AutoMapper.Mapper.Initialize(cfg =>
{
cfg.CreateMap<Opportunity, OpportunityLite>();
cfg.CreateMap<DealerOpportunity, DealerOpportunityLite>();
});
版本:4.2.1
如果我执行以下地图:
Opportunity oppLite = new Opportunity
{
DealerOpportunity = new DealerOpportunity
{
BranchCode = "00168"
}
};
var opp = AutoMapper.Mapper.Map<Opportunity, OpportunityLite>(oppLite);
“opp.DealerOpportunity.BranchCode”的属性值为:00168
从版本 5.0.2 到 6.2.2
在这些版本中,相同属性的值是:null
如何更新 AutoMapper 以获得与 4.2.1 版相同的结果,而无需在映射配置中添加“ForMember”?不幸的是,我有很多此类模式的案例。
解决方案
推荐阅读
- python - 文本小部件作为列表
- python - 如何配置 PuLP 以调用 GLPK 求解器
- python - [FORKING]Python 正则表达式 - Re.Sub 和 Re.Findall 有趣的挑战
- qgis - 有没有办法将某一年的瓦片地图(如谷歌或必应)加载到 Qgis 中?
- python - 使用 pip install 时,为什么 (from versions: xxx) 的列表很短?
- python - 为什么 Amazon SES 接受我的获取请求,但拒绝我的类似结构的发布请求?
- php - php如何从id获取标题url
- redis - 如何从 Redis 获取所有 LFU(最不常用)密钥?
- r - R:“x”值相同,而 lambda 值在 boxcoxnc 函数中的重新划分太重
- ruby-on-rails - Rails:使用 Rails 和 HTTParty 将 JSON 发布到 API