Hello
I have some logic whereby a graph of POCO entities need to be cloned and to do this I have created partial classes for each T4 generated self tracked entity and implemented ICloneable on each. Each entity can clone itself and iterates through all of its children in navigation properites and calls Clone() on each child entity, then finally returns a cloned instance of itself with its cloned children added. So it is a deep clone and works fine. In the cloned tree all Entities are in the Added state, which makes sense...as the cloned graph is all new entities.
The clone process ocours service-side within an entity context, though no data access is done by the clone process itself, the clone is performed on a newly retrieved a graph from the database (If there is a better way to clone an entity graph feel free to inform me...).
Now the cloned graph is then shipped to our Silverlight 4 application via WCF and is displayed there. The user can instantly save or add / modify data in this graph and then save it back to the service no problems, if however, they remove any nodes from the graph it is a problem.
When the user clicks a control to remove a node, I alter the entity graph by calling MarkAsDeleted(). This works fine for Unchanged graphs, but with my cloned graph with everything in the Added state, upon saving back to the database in the WCF service I am getting an OptimisicConcurrencyException.
"Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded. Refresh ObjectStateManager entries."
In the service I am calling
context.EntitySetX.ApplyChanges(entityNodeInTree);
context.SaveChanges();
This is all fine except in this one scenario. The work around for the user so far is to save the cloned graph back to the database, retrieve it again (now will all be Unchanged) and then delete any data they wish to and finally save again.
I do not understand why this is happening. Is it the case that you cannot MarkAsDeleted something that is in the added state? That doesnt really make sense to me as there are instances where items are both added and then removed from the graph during the use of our Silverlight app and this does not cause issues.
Any ideas?
Thanks