✅ EF Best way to do UPDATE on entity
Hello,
EF by default tracks entities. That means, if a property of an entity is modified,
SaveChanges()
will do the update.
Now, I set new values for the properties in the business logic layer, and the repository layer is responsible for any DB operation.
My current method looks like this.... Is there a clearer approach or leave this as it is?
17 Replies
Just don't add any kind of repositories on top of your DbContext. Just let your service deal with the DbContext directly.
Well, there is a quite large 100 row+ complex query for cursor pagination + filtering. And the app is built with N-tier architecture
What does that have to do with anything?
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
If the repository layer (the one wrapping the EF queries) is removed, then the code should go somewhere
Unknown User•4mo ago
Message Not Public
Sign In & Join Server To View
With repositories on top of EFC's repository (yes, EF/EFC already implement the Unit of Work and the Repositories), you lose key abilities like projection and dynamic filtering, unless you add in tons and tons of pass-through that you have to add in, with all the boilerplate that goes with it.
Yes, this is very true. Thanks for pointing it out
Just so you know, a LOT of people make this mistake.
I've done it too.
I have a middleware catching exceptions. Would it be reasonable for doing a similar solution for EF exceptions, so no try, catch blocks?
Where I'm working at now, there is a senior dev that has been using .Net for almost 20 years now, and they still want to use repositories to wrap EF 😛
If not wrapping, should I put them in private methods inside the logic layer?
This logic should generally be implemented at the service/logic level, since it's at that point that you know how to handle what.
If you want, you can let some exceptions through, like the DbUpdateConcurrency exception, and have these handled by a middleware.
I'm not certain of what specifically you're talking about, so it's hard to give an advice on that.
Thanks for clarifying this concept
It helped a lot
Great! Good luck! Gotta go to bed now 😛
$close
If you have no further questions, please use /close to mark the forum thread as answered