Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't have to load object before updating - check <a href="https://stackoverflow.com/questions/3594515/how-to-update-an-entity-in-entity-framework-4-net/3594608#3594608">this answer</a>. But if you want to work with related objects it is really useful. In your case the Address entity is probably related to Customer enity in one to many relation. If you want to modify customer with addresses without loading entity graph you have to know which addresses were modified, inserted and deleted. For one to many relation this can usually by simulated by these rules:</p> <ul> <li>Id = 0 means new entity</li> <li>Id > 0 means update (so you are updeting even if address didn't changed)</li> <li>Id &lt; 0 means delete (current Id is negation of deleted Id)</li> </ul> <p>You can see that this technique is not very good because you always update unchanged entities and you have to handle deletes in different way then just removing them from Addresses collection in Customer entity. Also this technique works only for aggregates where related enity cannot exists without parent entity. If you can delete only relation but the entity remains in database, this technique is useless and you need to find another change tracking mechanism - this is exactly what happens if you want to track changes in many to many relations. So that is the reason why I gave up this technique and why I load modified entites from database before I process changes. There is also another <a href="https://stackoverflow.com/questions/3635071/update-relationships-when-saving-changes-of-ef4-poco-objects/3635326#3635326">post</a> about this problem.</p> <p>If you don't want to track state by yourselves at all you can check <a href="http://msdn.microsoft.com/en-us/library/ff407090.aspx" rel="nofollow noreferrer">Self tracking entities</a> (STE) but be aware of their <a href="https://stackoverflow.com/questions/3814706/self-tracking-entities-vs-poco-entities/3815968#3815968">drawbacks</a>.</p> <p>Btw. delete operation can be also performed without loading the entity first. You can usually create dummy entity (empty) and set only its Id and concurrency handler (I'm always using timestamp). Then you can attach this entity to context and delete it. But this will not work if entity is in child relation to other entitity. In that case you also have to manually delete relation or exception will be thrown. This will not occure if you load the entity first from database.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload