Odd behavior saving entity from entity collection

Posts   
 
    
Posts: 93
Joined: 13-Feb-2008
# Posted on: 21-Apr-2008 19:53:29   

public static bool Save(EntityCollection<PaymentRequestEntity> payments)
        {
            bool success = true;
            List<PaymentRequestEntity> dirtyPayments = payments.DirtyEntities;

            using (CoreDataAccessAdapter adapter = new CoreDataAccessAdapter())
            {
                try
                {
                    PredicateExpression updateRestriction;
                    
                    adapter.StartTransaction(System.Data.IsolationLevel.ReadCommitted, "Update Status");

                    foreach (PaymentRequestEntity payment in dirtyPayments)
                    {
                        updateRestriction = new PredicateExpression(
                            PaymentRequestFields.UpdateDate == payment.UpdateDate);
                        updateRestriction.Add(PaymentRequestFields.UpdateBy == payment.UpdateBy);
                        
                        if (!adapter.SaveEntity(payment,
                            true,
                            updateRestriction))
                        {
                            success = false;
                            break;
                        }
                    }
                }
                catch
                {
                    success = false;
                    throw;
                }
                finally
                {
                    if (success)
                    {
                        adapter.Commit();
                    }
                    else
                    {
                        adapter.Rollback();
                    }
                }
            }

            return success;
        }

I get dirtyPayments.Count number of updates after the first call to adapter.SaveEntity. Basically all the dirty entities are updated on the first call, and to make it worse the updateRestriction is only included on the first update statement. It seems that the whole underlying collection is being saved when the first entity is saved. Any help will be appreciated.

Posts: 93
Joined: 13-Feb-2008
# Posted on: 21-Apr-2008 20:20:08   

It works when I set recursive to false. Apparently when the framework pulls the object graph for the recursive save the first entity is somehow (not immediately apparent) related to the rest of the entities in the collection. I'm pretty stumped right now to figure out how the recursive save is affecting seemingly unrelated entities. I'll try to step through the llblgen source and see what is going on there...Whats the easiest way to see what related entities will be saved for a root entity?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 21-Apr-2008 21:30:55   

The easiest way to calculate the save queues is by using the ObjectGraphUtils to calculate the two save queues. You can also add the entity to a UnitOfWork2 instance (save with recursion) and call ConstructSaveProcessQueues to create the two save queues and traverse them.

Frans Bouma | Lead developer LLBLGen Pro