CodeSmith Community
Your Code. Your Way. Faster!

Order of Handling DeletedItems during Save and DeepSave of Collections

rated by 0 users
This post has 1 Reply | 2 Followers

Not Ranked
Posts 8
Points 220
rodgel Posted: Thu, Apr 13 2006 1:07 PM
I ran into a problem when saving collections.  This problem occured due to deleting items from a TList and then adding a new item to the TList with the same primary key.  During the saving of this collection, it attempts to do the saves prior to the delete.  The inserted entity has a state of Added so it attempts to do an insert which fails with a primary key constraint error.

The code that is executing is as follows:
        public void Save(TransactionManager transactionManager, <%=collectionClassName%> entityCollection)
        {
            foreach (<%=className%> entity in entityCollection)
            {
                Save(transactionManager, entity);
            }
           
            foreach (<%=className%> entity in entityCollection.DeletedItems)
            {
                Delete(transactionManager, entity);
            }
           
            // Clear the items to delete list.
            entityCollection.DeletedItems.Clear();
        }

Is there a reason the DeletedItems are not handled first?  This would alleviate the problem I am having.  My current solution is either to change the templates or to use a workflow to Delete all of the children and then insert them all.
  • | Post Points: 35
Top 75 Contributor
Posts 80
Points 1,835
Polar replied on Fri, Oct 19 2007 2:23 PM

I just came across the same problem, but I have a unique index of a bunch of fields thats causing the error. It makes sense to process deletes first.

Page 1 of 1 (2 items) | RSS
Copyright © 2010 CodeSmith Tools, LLC