I am using Automapper to transfer data from objectA to objectB
classe ObjectA
{
string Title;
string Summary;
}
classe ObjectB
{
string Title;
string Summary;
IAddress Address;
}
I created this kind of mapping between the two objects
AutoMapper.Mapper.CreateMap<IObectA, IObjectB>()
.ForMember(dest => dest.Title, src => src.MapFrom(s => s.Title))
.ForMember(dest => dest.Summary, src => src.MapFrom(s => s.Summary))
.ForMember(dest => dest.Address, src => src.Ignore())
I create my ObjectB and fill all its properties including Address
When I call the mapper, I was expecting it to override Title and Summary and ignore Address
ObjectB = Mapper.Map<IObjectA, IObjectB>(objectA);
Actually, it is throwing and exception for Address.
What am I doing wrong?
[UPDATE] To express it differently, I have my objectB and I want to update part of it with data coming from ObjectA. When I say Ignore, I mean leave the data the way they already are
I found the solution.
I just discovered that Map method has an overloaded version that excepts a pre-instantiated destination object. Thanks to this article
Mapper.Map<IObjectA, IObjectB>(objectA, ObjectB );
@JoDG, Thank you for your help
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