Replies: 3 comments 2 replies
-
|
There are a few generic ways to do this and you're going to have to choose whatever works best for you. If you need help with that, you should ask on SO. |
Beta Was this translation helpful? Give feedback.
2 replies
-
|
I think this is better suited for StackOverflow. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
I'm a bit hesitant to add any more features to reverse mapping as well. It's a much harder problem to solve so I don't really want to add too many features to it. I'd like to promote just regular code. |
Beta Was this translation helpful? Give feedback.
0 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.
-
So I've been breaking my head over this. If you search for it, the internet is full of people with the same question, and all kinds of clunky solutions have been created.
A common scenario when using automapper combined with EF core, is that we want to update a bunch of properties on an entity, but only update properties that have a value.
So we have a source type that looks like this:
and a EF core Poco that looks like:
notice that in the
MutationCarall properties are nullable, and in theCarPocothey are not (because in the database these properties are required.Now we want to map a
MutationCaron aCarPocoto update the car in the database:To make this work, we have to avoid automapper trying to set destination members when the source member is null. However, we don't want to manually map each property individually and set null mapping properties, so we want to use
ForAllMembersand we have 2 options to use:PreConditionandCondition. The problem withPreconditionis that don't get any reference to the source member or value, so we cannot use this. TheConditionoption seems to work, but as stated in all kinds of earlier issues, it has the limitation when mapping from a nullable to a non-nullable value type, it converts null values of the source member to its default non-nullable variant (for for bool:nullbecomesfalse, for int,nullbecomes0etc.).Seeing the amount of issues for automapper and questions on stackoverflow, there seems to be a demand for a proper solution without having to use custom
ValueMemberResolverclasses. Ideally, any of the below:IMapperExpressioncalledSkipNullValuesor something, that accomplishes this behaviorConditioncallNow I'm probably willing to contribute to a proper solution for this, but only if it has a realistic change of getting merged. Therefore I would first like to discuss this, or see if there are other better alternatives for a generic solution before starting to create code for this
Beta Was this translation helpful? Give feedback.
All reactions