Deleting a modified entity

Jan 23, 2009 at 5:30 PM
                if (entity.LINQEntityState == EntityState.Deleted && !entitiesDeleted.Contains(entity))
                {

                    // Check to see if cascading deletes is allowed
                    if (cascadeDelete)
                    {
                        // Grab the entity tree and reverse it so that this entity is deleted last
                        List<LINQEntityBase> entityTreeReversed = entity.ToEntityTree();
                        entityTreeReversed.Reverse();

                        // Cascade delete children and then this object
                        foreach (LINQEntityBase toDelete in entityTreeReversed)
                        {
                            //padeb
                            if (!entitiesDeleted.Contains(toDelete))
                            {
                                //pafin
                                toDelete.SetAsDeleteOnSubmit();
                                LINQEntityBase entityToDelete = toDelete.LINQEntityOriginalValue == null ? toDelete : toDelete.LINQEntityOriginalValue;
                                targetDataContext.GetTable(toDelete.GetEntityType()).Attach(entityToDelete);
                                targetDataContext.GetTable(toDelete.GetEntityType()).DeleteOnSubmit(entityToDelete);
                                //padeb
                                entitiesDeleted.Add(toDelete);
                            }
                            //pafin

                        }
                        //add these to a list to make sure we don't attach them twice.
                        //padeb
                        //entitiesDeleted.AddRange(entityTreeReversed);
                        //pafin
                    }
                    else
                    {
                        LINQEntityBase entityToDelete = entity.LINQEntityOriginalValue == null ? entity : entity.LINQEntityOriginalValue;
                        targetDataContext.GetTable(entity.GetEntityType()).Attach(entityToDelete);
                        targetDataContext.GetTable(entity.GetEntityType()).DeleteOnSubmit(entityToDelete);
                        entitiesDeleted.Add(entity);
                    }

                    // if this is the root object, there's no need to do more processing
                    // so just quit the loop
                    if (this == entity)
                        break;
                }

Hi Matt,

We had a problem with a modifed entity that we want to delete later. We added the bold lines in our code.

Patrick