C
C#13mo 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
Jimmacle13mo ago
what's the high level goal you're trying to accomplish?
Nico
Nico13mo 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
Jimmacle13mo 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
Jimmacle13mo 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
Nico13mo 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
Nico13mo 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
boiled goose
boiled goose13mo 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
Nico13mo ago
EF Core automatically keeps the objects' states in sync. So after updating on object, the other one will also update
boiled goose
boiled goose13mo ago
not if it's from another dbcontext instance
Nico
Nico13mo ago
Ah okay, thank you. I will try that
boiled goose
boiled goose13mo ago
(and eventually read with .AsNoTracking())
Nico
Nico13mo ago
@chaos_solo I tried it and got it to work! Such a simple solution and I wasted so much time 😂 Thank you :)
boiled goose
boiled goose13mo ago
remember to /close this if you don't need help anymore
Want results from more Discord servers?
Add your server
More Posts