Replies: 1 comment 3 replies
-
I think this is better suited for StackOverflow. |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
In our API, we have an "UpdateRequest" that we use for PATCH and PUT api requests.
The gist of it is that all properties in requests inheriting from UpdateRequest have nullable properties that reflect the (not necessarilly nullable) properties in the db entity.
If a user only wants to update a certain property, like
foo
, they don't need to populate the rest of the properties, as null properties use the existing db value instead.If they want to null out a property however, they can add it to the
Metadata
dictionary with the correct property name.This is handled in the
MapUpdateRequest
mapping expression in Mapping Configuration below.The fiddly bit we have at the moment, is when we have two properties with matching names, but incompatible types, like the
Contacts
lists inCustomerUpdateRequest
andCustomer
.We do some extra processing outside of automapper with the Contacts list (we create/delete CustomerContact db records so it matches the IDs passed in the list), and so we don't want automapper to map this property.
We have tried using
ForMember(c => c.Contacts, opt => opt.Ignore())
andForMember(c => c.Contacts, opt => opt.UseDestinationValue())
but it looks like the
ForAllMembers
in ourMapUpdateRequest
method is causing automapper to try to configure the mapping, as it throwsAutoMapper.AutoMapperMappingException
when we runvar mappedModel = Mapper.Map(customerUpdateRequest, existingCustomer);
I have had a look through docs, unit tests, and stackoverflow and I can't find a solution to this (I might be missing something though)
The closest I could find was the deprecated "ForAllOtherMembers".
Please let me know if you need more info/context/etc.
Thanks in advance :)
Source/destination types
Mapping configuration
Version: 12.0.1
Expected behavior
the Contacts collections would be skipped
Actual behavior
AutoMapper.AutoMapperMappingException is thrown, as we can't map an Int to a Contact
Beta Was this translation helpful? Give feedback.
All reactions