Henrik, thank you for the response.
I just want to explain that while you are right about the first way, while the second one is possible only if a few conditions are fulfilled.
In OpenAccess, dbContext.Delete(...) marks the object for removal and to undo it before the transaction (to roll it back) you can use dbContext.ClearChanges() (more about this method you can read here
). Note that, it will cancel all pending changes from the context (including dbContext.Delete(..)) and if you intend to use it, be very careful with context management.
Adding back an object
In OpenAccess, the lifecycle of the objects you make persistent by calling dbContext.Add(...) is as follows: transient -> persistent new -> persistent new deleted or hollow. That means that when you create an object in your code, add it to the context, mark it for removal and than add it again, the second dbContext.Add(...) command will be ignored. In other words you cannot do
in the same transaction.
The other scenario when you retrieve an existing object from the database, mark it for removal and then add a new object with the same property values, is possible and it looks roughly like this (using the SofiaCarRental database):
(EntityDiagrams dbContext =
//Retrieve an object from the database
"TE 9876 IK"
Car carToDelete = dbContext.Cars.FirstOrDefault(c => c.TagNumber == carTagNumber);
//Add some code to preserve the values
//of the properties of carToDelete
carModel = carToDelete.Model;
//carToDelete is marked for delete and the values
//of all its properties are set to NULL
//An object with properties equal to the properties
//of carToDelete is created
Car carToAdd =
TagNumber = carTagNumber,
Model = carModel
//Add values for the rest of the properties of carToAdd
//Add carToAdd in the context
//Once you save the changes the delete statement will be executed
//and after that the new object will be added to the database with
//the same property values.
For additional information about the lifecycle of the objects in OpenAccess, I suggest you to check here
If you have any other questions or you are trying to implement another scenario, do not hesitate to get back to us.
All the best,
the Telerik team
Follow @OpenAccessORM Twitter channel to be the first one to get the latest updates on new releases, tips and tricks and sneak peeks at our product labs!