I am trying to use automapper to create a single map for a parent object and to reuse this amongst its children.
For child properties I only want to map the extra fields.
Is this possible? The code I have looks like this
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
Why does it keep telling me the parents properties are not mapped? Yet, I thought I included them in CreateCalculationMap(message); which contains
Mapper.CreateMap<QuoteMessage, CalculationBase>() ()
Yes, or you can call CreateMap<ModelClass, ViewModelClass>(). ReverseMap() .
The Automapper Reverse Mapping is nothing but the two-way mapping which is also called as bidirectional mapping. As of now, the mapping we discussed are one directional means if we have two types let's say Type A and Type B, then we Map Type A with Type B.
How do I use AutoMapper? First, you need both a source and destination type to work with. The destination type's design can be influenced by the layer in which it lives, but AutoMapper works best as long as the names of the members match up to the source type's members.
So, the AutoMapper Ignore() method is used when you want to completely ignore the property in the mapping. The ignored property could be in either the source or the destination object.
FYI I figured this out
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With