C# 自动映射继承--重用映射
我试图使用automapper为父对象创建一个映射,并在其子对象中重用该映射 对于子属性,我只想映射额外的字段 这可能吗?我的代码如下所示C# 自动映射继承--重用映射,c#,automapper,C#,Automapper,我试图使用automapper为父对象创建一个映射,并在其子对象中重用该映射 对于子属性,我只想映射额外的字段 这可能吗?我的代码如下所示 CreateCalculationMap(message); ---- This does the BASE parent mappping. Mapper.CreateMap<QuoteMessage, CalculationGipMessage>() -- This is the child.
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(消息);--这将执行基本父映射。
Mapper.CreateMap()--这是子对象。
.Include()--是否包含父级?
.ForMember(a=>a.OngoingCommission,b=>b.MapFrom(c=>c.OngoingASF))-Child
.ForMember(a=>a.SpecialRate,b=>b.MapFrom(c=>c.BlahBlah));-小孩
为什么它总是告诉我父属性没有映射?然而,我认为我把它们包括在CreateCalculationMap(消息)中;其中包含
Mapper.CreateMap<QuoteMessage, CalculationBase>() ()
Mapper.CreateMap()()
我不认为您不能这样做-您需要将映射放在一起。这实际上可能是一个bug,因为wiki声称这是可以做到的——但是给出的示例只是依赖属性的名称来进行映射,而不是includes位。至少我是这样理解的
如果查看并更改Source和Dest中属性的名称(我将它们设置为值3和值4以真正混淆),那么请明确添加映射
Mapper.CreateMap<ChildSource, ChildDestination>()
.ForMember( x => x.Value4, o => o.MapFrom( y => y.Value2 ) );
Mapper.CreateMap<ParentSource, ParentDestination>()
.Include<ChildSource, ChildDestination>()
.ForMember( x => x.Value3, o => o.MapFrom( y => y.Value1 ) );
Mapper.CreateMap()
.ForMember(x=>x.Value4,o=>o.MapFrom(y=>y.Value2));
Mapper.CreateMap()
.包括()
.ForMember(x=>x.Value3,o=>o.MapFrom(y=>y.Value1));
然后它似乎失败了
ChildSource s = new ChildSource()
{
Value2 = 1,
Value1 = 3
};
var c = s.MapTo<ChildDestination>();
var c2 = s.MapTo<ParentDestination>();
Assert.AreEqual( c.Value3, s.Value1 );
Assert.AreEqual( c.Value4, s.Value2 );
Assert.AreEqual( c2.Value3, s.Value1 );
Assert.AreEqual( c.Value4, s.Value2 );
ChildSource s=new ChildSource()
{
值2=1,
值1=3
};
var c=s.MapTo();
var c2=s.MapTo();
主张平等(c.Value3,s.Value1);
主张平等(c.Value4,s.Value2);
断言。等于(c2.Value3,s.Value1);
主张平等(c.Value4,s.Value2);
其他注释
此外,Include必须是子级而不是父级。原型实际上说明了这一点
public IMappingExpression<TSource, TDestination> Include<TOtherSource, TOtherDestination>()
where TOtherSource : TSource
where TOtherDestination : TDestination
public IMappingExpression Include()
where TOtherSource:TSource
目的地:t目的地
根据我所读到的内容,您应该首先创建您的子映射,尽管这可能是一个老问题
Mapper.CreateMap<ChildSource, ChildDest>();
Mapper.CreateMap();
那你的父母呢
Mapper.CreateMap<ParentSource, Parent Dest>()
.Include<ChildSource, ChildDest>();
Mapper.CreateMap()
.Include();
来源于供参考我发现了这一点
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
公共静态IMappingExpression ApplyBaseQuoteMapping(此IMappingExpression IMappingExpression)
其中A:QuoteMessage
其中T:CalculationGipMessage
{
图像表达
.ForMember(a=>a.LoginUserName,b=>b.MapFrom(c=>c.LoginUserName))
.ForMember(a=>a.assettetextemptb,b=>b.Ignore())
;
返回iMappingExpression;
}
Mapper.CreateMap()
.ApplyBaseQuoteMappingToOldCol()
//在此处执行其他映射