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, I have some code that first does a move, and then does some inserts. However, upon calling SaveChanges - the SQL that is run, is the inserts first, followed by the delete (which is deleting the rows that were just inserted) Is this normal? Or am I missing something? Below is an example of the code - the SQL generated is all correct, just not in the order it's executed. Cheers,
var toRemove = new Query { EntityType = typeof(Content), QueryExpression = Entity.Attribute("fk_contentID") == id }; UnitOfWork.Remove(toRemove); for (int index = 0; index < 5; index++) var x = new Content { Fk_SomeId = index , Fk_ContentId = index , Order = index }; UnitOfWork.Add(x); } UnitOfWork.SaveChanges(); |
|
|
This is normal. When you call SaveChanges() you are asking LightSpeed to update the state in the database to reflect the current working state of the UnitOfWork. LightSpeed does this by inspecting the state of all the known entities it has loaded within that UnitOfWork and then performs inserts, updates and deletes to achieve that syncronisation. It does not keep track of the "order" in which you performed them, because for example if you update an entity twice, it would not be as efficient to send down two seperate update statements when a single statement can achieve the same result. Likewise if you update and then delete an entity it should just be deleted :) In your scenario above, you should flush the removal query at the time by calling SaveChanges immediately after the UnitOfWork.Remove(). Anytime you want LightSpeed to act on things in a specific serial nature, just include explicit flushes to enact that ordering.
Jeremy |
|