You do not need to download the object before updating - check this answer . But if you want to work with related objects, this is really useful. In your case, the Address object is probably related to Customer enity in a one-to-many relationship. If you want to change the client with addresses without loading the graph of objects, you need to know which addresses have been changed, inserted and deleted. For a one-to-many relationship, this can usually be modeled with the following rules:
- Id = 0 means a new object
- Id> 0 means update (so you update even if the address has not changed)
- Id <0 means delete (current identifier is the negation of the deleted identifier)
You can see that this method is not very good, because you always update unchanged objects, and you have to handle deletions differently, and then just delete them from the Addresses collection in the Customer object. Also, this method only works for aggregates where a linked community cannot exist without a parent entity. If you can only delete the relationship, but the object remains in the database, this method is useless, and you need to find another mechanism for tracking changes - this is exactly what happens if you want to track changes in many ways. This is why I abandoned this technique and why I load the changed data from the database before processing the changes. There is also another message about this issue.
If you donβt want to monitor the status yourself, you can check the self-examination objects (STEs), but keep in mind their shortcomings .
Btw. a delete operation can also be performed without first loading the object. Usually you can create a dummy object (empty) and set only its Id and concurrency handler (I always use timestamp). Then you can attach this object to the context and delete it. But this will not work if the subject is in a childlike relationship to another resolution. In this case, you also have to manually delete the relation, otherwise an exception will be thrown. This will not happen if you first load the object from the database.
source share