protected override void Update()
        {
            if (m_usePagedActions)
            {
                PagedCollection <IMigrationAction> pagedActions = this.Actions as PagedCollection <IMigrationAction>;
                pagedActions.SaveActivePage();
            }

            using (RuntimeEntityModel context = RuntimeEntityModel.CreateInstance())
            {
                RTChangeGroup rtChangeGroupCache = context.GetObjectByKey(m_runTimeChangeGroup.EntityKey) as RTChangeGroup;

                switch (Status)
                {
                case ChangeStatus.InProgress:
                    rtChangeGroupCache.StartTime = DateTime.UtcNow;
                    break;

                case ChangeStatus.DeltaComplete:
                case ChangeStatus.Complete:
                    rtChangeGroupCache.FinishTime = DateTime.UtcNow;
                    break;

                default:
                    break;
                }
                rtChangeGroupCache.Status = (int)Status;
                rtChangeGroupCache.ReflectedChangeGroupId = ReflectedChangeGroupId;
                populateMetaData(rtChangeGroupCache);
                context.TrySaveChanges();
            }
        }
        private RTChangeGroup getNativeRTChangeGroup(RuntimeEntityModel context)
        {
            if (null != m_runTimeChangeGroup &&
                null != m_runTimeChangeGroup.EntityKey)
            {
                return(context.GetObjectByKey(m_runTimeChangeGroup.EntityKey) as RTChangeGroup);
            }
            else if (this.ChangeGroupId > 0)
            {
                var changeGroupQuery = context.RTChangeGroupSet.Where(cg => cg.Id == ChangeGroupId);
                if (changeGroupQuery.Count() > 0)
                {
                    return(changeGroupQuery.First());
                }
            }

            throw new InvalidOperationException(MigrationToolkitResources.ErrorMissingParentChangeGroup);
        }
        internal void PersistCurrentStatus(RuntimeEntityModel context)
        {
            RTChangeGroup rtChangeGroupCache = context.GetObjectByKey(m_runTimeChangeGroup.EntityKey) as RTChangeGroup;

            switch (Status)
            {
            case ChangeStatus.InProgress:
                rtChangeGroupCache.StartTime = DateTime.UtcNow;
                break;

            case ChangeStatus.DeltaComplete:
            case ChangeStatus.Complete:
                rtChangeGroupCache.FinishTime = DateTime.UtcNow;
                break;

            default:
                break;
            }
            rtChangeGroupCache.Status = (int)Status;
        }
        /// <summary>
        /// bulk save sliced child change actions
        /// </summary>
        /// <param name="page">a page/slice of child change actions</param>
        internal void BatchSaveChangeActions(IList <IMigrationAction> page)
        {
            Debug.Assert(m_runTimeChangeGroup != null);

            const int bulkChangeActionInsertionSize = 1000;
            int       changeActionCount             = 0;
            int       pageIndex = 0;

            while (pageIndex < page.Count)
            {
                using (RuntimeEntityModel context = RuntimeEntityModel.CreateInstance())
                {
                    RTChangeGroup rtChangeGroupCache = getNativeRTChangeGroup(context);
                    Debug.Assert(null != rtChangeGroupCache);

                    while (pageIndex < page.Count && changeActionCount < bulkChangeActionInsertionSize)
                    {
                        SqlMigrationAction action = page[pageIndex] as SqlMigrationAction;
                        Debug.Assert(null != action);

                        // cache "Dirty" flag, as assignin 'this' to its ChangeGroup will set the flag to true
                        bool actionWasDirty = action.IsDirty;
                        action.ChangeGroup = this;

                        if (!action.IsPersisted)
                        {
                            IMigrationItemSerializer serializer = ManagerWithMigrationItemSerializers[action.Action];
                            action.CreateNew(serializer);
                            context.AddToRTChangeActionSet(action.RTChangeAction);
                            action.RTChangeAction.ChangeGroup = rtChangeGroupCache;
                            ++changeActionCount;
                        }
                        else if (actionWasDirty)
                        {
                            IMigrationItemSerializer serializer     = ManagerWithMigrationItemSerializers[action.Action];
                            RTChangeAction           rtChangeAction = action.RTChangeAction;
                            if (null != rtChangeAction)
                            {
                                rtChangeAction = context.GetObjectByKey(rtChangeAction.EntityKey) as RTChangeAction;
                            }
                            else
                            {
                                rtChangeAction = context.RTChangeActionSet.Where(ca => ca.ChangeActionId == action.ActionId).First();
                            }

                            rtChangeAction.Recursivity           = action.Recursive;
                            rtChangeAction.IsSubstituted         = action.State == ActionState.Skipped ? true : false;
                            rtChangeAction.ActionId              = action.Action;
                            rtChangeAction.SourceItem            = serializer.SerializeItem(action.SourceItem);
                            rtChangeAction.ToPath                = action.Path;
                            rtChangeAction.ItemTypeReferenceName = action.ItemTypeReferenceName;
                            rtChangeAction.FromPath              = action.FromPath;
                            rtChangeAction.Version               = action.Version;
                            rtChangeAction.MergeVersionTo        = action.MergeVersionTo;

                            if (action.MigrationActionDescription != null &&
                                action.MigrationActionDescription.DocumentElement != null)
                            {
                                rtChangeAction.ActionData = action.MigrationActionDescription.DocumentElement.OuterXml;
                            }
                            ++changeActionCount;
                        }

                        ++pageIndex;
                    }
                    context.TrySaveChanges();
                    changeActionCount = 0;
                }
            }
        }