C
C#•2y ago
Nico

EF Core: Keep Copy of Entity with old state when updating

Hello, I am trying to update an entity and keep its previous state as a copy disconnected from the databaseContext. After the update I want to use the copy and the updated object for comparisons. I already tried all sorts of things but couldn't find a working solution so far. I would love to have a generic solution for this problem. Sadly, serializing the object that is to be updated before the update didn't work with JsonConverter and BinaryFormatter.
13 Replies
Jimmacle
Jimmacle•2y ago
what's the high level goal you're trying to accomplish?
Nico
NicoOP•2y ago
I want to update the entity and send an email that shows which properties of the entity changed. I use Razor-Templates to create the email at runtime and therefore need the old and the new state of the entity to fill the template with information
Jimmacle
Jimmacle•2y ago
have you considered using EF's change tracker? https://learn.microsoft.com/en-us/ef/core/change-tracking/
Change Tracking - EF Core
Overview of change tracking for EF Core
Jimmacle
Jimmacle•2y ago
this article has some more details on specifically using the change tracker to find which properties of an entity changed https://codewithmukesh.com/blog/audit-trail-implementation-in-aspnet-core/
Nico
NicoOP•2y ago
Yes, I previously tried building my own generic function using the change tracker. However, since my entities refer to each other bidirectionally, I ran into problems avoiding circular dependencies in my recursion. I found a way to handle it for one to one relationships, but eventually gave up because my solution didn't work for collections. So I would still get loops in my recursion for many to one and many to many relationships. I thought that copying the object somehow would be an easier way to implement this feature and also easier to work with
Nico
NicoOP•2y ago
The documentation here even says that you can ignore ReferenceLoops using Json.NET but in practice in doesn't work.. it can't seem to deal with proxies from ef core :( https://learn.microsoft.com/en-us/ef/core/querying/related-data/serialization
Related Data and Serialization - EF Core
Information about how cycles in related data with Entity Framework Core can affect serialization frameworks
Omnissiah
Omnissiah•2y ago
why don't you just get the object for the db two times, you use one to du the update, the second one for the comparison or maybe i didn't understand well
Nico
NicoOP•2y ago
EF Core automatically keeps the objects' states in sync. So after updating on object, the other one will also update
Omnissiah
Omnissiah•2y ago
not if it's from another dbcontext instance
Nico
NicoOP•2y ago
Ah okay, thank you. I will try that
Omnissiah
Omnissiah•2y ago
(and eventually read with .AsNoTracking())
Nico
NicoOP•2y ago
@chaos_solo I tried it and got it to work! Such a simple solution and I wasted so much time 😂 Thank you :)
Omnissiah
Omnissiah•2y ago
remember to /close this if you don't need help anymore
Want results from more Discord servers?
Add your server