❔ Using automapper to update EF Core entities recursively
I'm trying the neat idea to try and make a generic create/update method which supports includes.
The first branch takes care of inserts. It takes requests of this form (currently doesn't check the nested ids, which it should):
It then maps this dto to a recursively tracked entity.
The second branch works with requests of this form, taking care of updates:
Currently I'm doing a bunch of things that are related to nested entities manually. I'm manually sorting the projects in reverse order, so that the projects with null key (projects to be inserted), end up at the end of the list. I'm then querying the entity from the db, checking if all of the projects meant for updating were found, and then I do a recursive copy of the dto into that entity. This maps all properties of the person and of each nested project. At least it should, what actually happens is it copies the null values over as well (see the mapper profile below). Then the tracker picks up on all of the newly mapped projects.
My question is, why does automapper map null properties even though it's set to ignore them, and is there a way to generalize this thing? The way I came up with doesn't seem overly complex, it's just not fully generic. I want the both the include filtering and the mappings to work independent of the level of depth of the objects.
12 Replies
(the check
!= default
should disallowed by validation, but I'm just trying things rn)
The request dtos have nullable ids, the entities have normal ids
The automapper behavior is weird:
- It doesn't track new elements with the same id. So it seems to be keeping the elements already present in the list;
- It does reset the projectName
to null
prior to checking the condition in UnmapAllNulls
, while ending up checking it afterward. Since it doesn't create new elements if they're already present in the collection, it must be resetting the instances. But I'm gonna make extra sure;
ah yeah it does recreate the elementsGitHub
GitHub - AutoMapper/AutoMapper.Collection: AutoMapper support for u...
AutoMapper support for updating existing collections by equivalency - GitHub - AutoMapper/AutoMapper.Collection: AutoMapper support for updating existing collections by equivalency
it's weird why ef core doesn't yell at me that there are tracked entities with the same ids
No part of this idea seems neat, it's seems awful
well, if that can be made generic, it could both do complex, nested updates in a single query to the server, and for all types that exist for the sake of CRUD at once. I'm hoping something like this can be achieved with graphql, reading the docs rn
ya its still an awful idea, you have somehow made automapper (by itself an awful idea) and made it worse lol
well thousands of lines of boilerplate for 100 types vs one manageable method might be a good tradeoff
no it wont be, you lose all type safety
i have spent the past year ripping automapper out of our api for that very reason
do you mean like if the mapping has not been defined?
no, i mean the mapping is not type safe lol
to me, checking if all maps resolve with a test is good enough
but that's to me
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.