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
|
Hi, Calling Uow.SaveChanges() will save all entities that are modified. Is there way to save just a single entity? Thanks. -chris |
|
|
No. A unit of work represents a business transaction, and should never be partially saved (integrity). If you have a business transaction that modifies only one entity, just have your unit of work modify only that one entity. Don't forget that you can save as you go along, e.g.
This avoids the cost of reloading entities or recreating connections. |
|
|
Hi, Ivan, I understand that SaveChange only save changed entities, however, there is no way to save EntityB without saving EntityA first. If EntityA and EntityB are related, it makes to save them at the same time for integrity reason, but if they are not related, it should be able to save independently. It is related to "Undo" feature I'm working on. I'm trying to undo changes to a certain entity, but this way(SaveChanges will save all entities), making undo really difficult (undoing from saved db vs. from memory) Thanks -chris. |
|
|
Hi Chris, If you want to save just one entity (which would be the unit of work you want your application to undertake at that point) you will need to create a new unit of work and attach the entity you wish to save to that and call .SaveChanges() on it. A unit of work is just a container for undertaking, as the name suggests, a unit of work. If your unit of work is to save only one entity then that should be all you have attached to it. I hope that helps :-) John-Daniel |
|
|
Ah.. I see.. that's how Uow works. I'll try with multiple Uows. Just one question though. If an Entity is spans multiple Uows(modified and saved from the different Uows), will it have conflicts? Thanks. -chris |
|
|
An entity instance is always attached to only one unit of work. You will need to call .Attach() to re-attach it to other units of work (if you add related entities to a unit of work LightSpeed will automatically attach all the associated loaded entities in the object graph to save you having to do this). I hope that helps :-) John-Daniel |
|
|
Hmm... I see. The following is what I need to do then.
Is this correct? Thanks. -chris |
|
|
If it's correct, why not have this feature implemented into LightSpeed? Something like Entity.Save()... and it will save the entity and its related entities. It will implicitely create a temporary Uow, Attach, SaveChanges, Attach back to original Uow. It's easy to implment and probably useful feature in other situation too. Thanks. -chris |
|
|
We don't have it because we're not looking to create 5 ways to do the same thing :-) Your situation is unique to your application and altering the product for specific applications of LightSpeed simply makes the API more confusing to end users if it tries to do everything. I would recommend creating an extension method, something like:
This way your application has a nice simple way of doing it with what LightSpeed provides already. I hope that helps :-) John-Daniel |
|
|
Sure.. Thanks -chris |
|