Saving changes to related entity in PerformWork

Posts   
 
    
tprohas
User
Posts: 257
Joined: 23-Mar-2004
# Posted on: 23-Apr-2008 20:35:36   

LLBLGen 2.5 Runtime: v2.0.50727 ASP.NET 2.0

I'm trying to make changes to a related entity when the root entity is being saved in the PerformWork event of the LLBLGenDataSource2 control. The code works but the update is never sent to the database. In the following code I'm also wondering why the IsDirty property on the visit request entity is never true.


protected void eventDetailsDS_PerformWork(object sender, PerformWorkEventArgs2 e)
{
    // LLBLGen really makes this kind of work a lot more fun.

    foreach (UnitOfWorkElement2 element in e.Uow.GetEntityElementsToUpdate())
    {
        ScheduledVisitEntity scheduledVisit = element.Entity as ScheduledVisitEntity;
        
        // Get visit requests related to this scheduled visit and update their properties.
        
        // First, let's see if we have a related visit request entity.
        if (scheduledVisit.VisitRequest.Count > 0)
        {
            // Get a reference to each visit request going to this event.
            foreach (VisitRequestEntity visitRequest in scheduledVisit.VisitRequest)
            {
                // Now find all the scheduled visit fields which have changed.
                foreach (IEntityField2 scheduledVisitField in scheduledVisit.Fields)
                {
                    if (scheduledVisitField.IsChanged)
                    {
                        // If this field exists in the visit request entity then update the value in visit request.
                        if (visitRequest.Fields[scheduledVisitField.Name] != null)
                        {
                            visitRequest.Fields[scheduledVisitField.Name].CurrentValue = scheduledVisit.Fields[scheduledVisitField.Name].CurrentValue;
                            // Will updating the visit request this way cause it to be saved with the scheduled visit?
                        }
                    }
                }

                if (visitRequest.IsDirty)
                {
                    // Why is the entity not dirty at this point?
                    e.Uow.AddForSave(visitRequest);
                }
            }
        }

        // We might want to check these two difference ways of updating the visit request and see which is more efficient.
        //ServiceManager.GetVisitProgramManager().CopyEventToRequest(scheduledVisit, visitRequest);
    }
    
    ServiceManager.GetPersistanceManager().UnitOfWorkCommit(e.Uow, true);
}

The UnitOfWork commit only saves the ScheduledVisitEntity. How can I make each of the related VisitRequestEntity's get saved as well?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Apr-2008 05:06:37   

Try change this

visitRequest.Fields[scheduledVisitField.Name].CurrentValue = scheduledVisit.Fields[scheduledVisitField.Name].CurrentValue;

with this:

visitRequest.SetNewFieldValue(scheduledVisitField.Name, scheduledVisit.Fields[scheduledVisitField.Name].CurrentValue);

_SetNewFieldValue _fires validation/auditing routines and set the corresponding field flags.

David Elizondo | LLBLGen Support Team