This thread looks to be a little on the old side and therefore may no longer be relevant. Please see if there is a newer thread on the subject and ensure you're using the most recent build of any software if your question regards a particular product.
This thread has been locked and is no longer accepting new posts, if you have a question regarding this topic please email us at support@mindscape.co.nz
|
I'm experencing problems when updating an existing entity. I create an entity, add it to the repository, save and then complete the unit of work. If I then modify the entity again, readd it to the repository and save, instead of updating the existing record, LightSpeed seems to try and reinsert it, which causes a primary key exception.
|
|
|
Add is for adding a new entity to the database. After a unit of work has been completed any existing entity references are effectively disconnected and therefore no longer tracked by LightSpeed. Rather than completing the unit of work, SaveChanges can be used to flush pending updates without clearing the current unit of work. Alternatively, you need to re-retrieve the entities from the Repository. Cheers, Andrew. |
|
|
Ok. I was under the impression Add added the entity to the current unit of work and LightSpeed then figured out what to do with the entity (add/update). It just seems unnecessary to me to have to re-retrieve an entity to update it when I already have that entity sitting around. |
|
|
[quote user="James Newton-King"]It just seems unnecessary to me to have to re-retrieve an entity to update it when I already have that entity sitting around.[/quote] The goal of the unit of work pattern is to encompass a logical set of operations against the database. Bear in mind that it's usually quite possible for other users/transactions to be working with the same data so the longer we hold on to it, the more likely it is that our copy is out of date. Thus, it usually makes sense to retrieve a clean, up-to-date copy when we begin a new unit of work. That said, I'm am going to put a bit more thought into "reattaching" entities this week. Cheers, Andrew. |
|