I am working with AutoMapper and some of the values for the entity being mapped to are variables in my current method. I have tried to Google it but to no avail. Can I pass a set of KeyValue Pairs or an object or something to my mapping to have it use those values?
//comment variable is a Comment class instance
var imageComment = AutoMapper.Mapper.Map<Data.ImageComment>(comment);
//I want to pass in imageId so I dont have to manually add it after the mapping
imageComment.ImageId = imageId;
AutoMapper handles this key-value pair scenario out of the box.
Mapper.CreateMap<Source, Dest>()
.ForMember(d => d.Foo, opt => opt.ResolveUsing(res => res.Context.Options.Items["Foo"]));
Then at runtime:
Mapper.Map<Source, Dest>(src, opt => opt.Items["Foo"] = "Bar");
A bit verbose to dig into the context items but there you go.
Is there any ways to use the ResolveUsing with the ConstructUsing method?
Is it possible the
ResolveUsing
method no longer accepts a lambda with just the context as argument or am I missing an extension function? I had to define a lambda with(input, output, memberValue, context)
as arguments before I could get it to work with Automapper 5.2. Of course, it could still be the compiler which can no longer figure out which overload it needs to use...5.x added overloads, and those overloads were adjusted so that you wouldn't get weird compile-time errors.
I get an error with res.Context - the source model doesn't contain a definition for context. Using .net core 2.0 and AutoMapper 6.2.1