C
C#2y ago
bookuha

❔ Automapper map Id to Entity

I receive the following DTO (request) BookDTO{ int Id, int AuthorId } I want the mapper to map AuthorId to the Author entity and put in into the Book.Author. How do I do this? Should I inject the dbcontext into my profiles? Or can I simply map Dto AuthorId to Entity AuthorId and it will be fine and navigation will be filled once context is saved? What is the best way?
11 Replies
Patrick
Patrick2y ago
automapper has nothing to do with that, you'd just set the author id on the book entity and not even use automapper
bookuha
bookuhaOP2y ago
But If I dont want to do this manually? I mean yes I get that this is the way it can be done without automapper
Patrick
Patrick2y ago
you're saving one line of code you do realise this
bookuha
bookuhaOP2y ago
yes, but there will be more properties
Patrick
Patrick2y ago
var book = context.Books.Single(x => x.Id == dto.Id);
book.AuthorId = dto.AuthorId;
var book = context.Books.Single(x => x.Id == dto.Id);
book.AuthorId = dto.AuthorId;
bookuha
bookuhaOP2y ago
i once tried to have the full mapping in my services ok. so should i map plain (strings, etc) properties with automapper and the complex ones (id to entity) in my services? is it how its done usually?
Patrick
Patrick2y ago
that's opinion; i dont recommend automapper at all, ever
bookuha
bookuhaOP2y ago
because of poor perf? and reflection being unintuitive
Patrick
Patrick2y ago
because it's a magic box and you have no idea what's going on, property name changes requires as much as configuration as you just mapping properties yourself and people end up testing if automapper maps stuff
bookuha
bookuhaOP2y ago
hmm yeah thank you
Accord
Accord2y ago
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.
Want results from more Discord servers?
Add your server