Ejemplo n.º 1
0
 internal async Task PersistAsync(PersistReason reason, CancellationToken token = default) =>
 await Persist(reason, true, token).ConfigureAwait(false);
        internal void Persist(PersistReason reason)
        {
            EnsureNotDisposed();
            if (IsPersisting || EntityChangeRegistry.Count == 0)
            {
                return;
            }
            EnsureAllAsyncQueriesFinished();

            var performPinning = pinner.RootCount > 0;

            if (performPinning || (disableAutoSaveChanges && !Configuration.Supports(SessionOptions.NonTransactionalEntityStates)))
            {
                switch (reason)
                {
                case PersistReason.NestedTransaction:
                case PersistReason.Commit:
                    throw new InvalidOperationException(Strings.ExCanNotPersistThereArePinnedEntities);
                }
            }

            if (disableAutoSaveChanges && reason != PersistReason.Manual)
            {
                return;
            }

            using (var ts = OpenTransaction(TransactionOpenMode.Default, IsolationLevel.Unspecified, false)) {
                IsPersisting       = true;
                persistingIsFailed = false;
                SystemEvents.NotifyPersisting();
                Events.NotifyPersisting();
                try {
                    using (this.OpenSystemLogicOnlyRegion()) {
                        DemandTransaction();
                        if (IsDebugEventLoggingEnabled)
                        {
                            OrmLog.Debug(Strings.LogSessionXPersistingReasonY, this, reason);
                        }

                        EntityChangeRegistry itemsToPersist;
                        if (performPinning)
                        {
                            pinner.Process(EntityChangeRegistry);
                            itemsToPersist = pinner.PersistableItems;
                        }
                        else
                        {
                            itemsToPersist = EntityChangeRegistry;
                        }

                        if (LazyKeyGenerationIsEnabled)
                        {
                            RemapEntityKeys(remapper.Remap(itemsToPersist));
                        }
                        ApplyEntitySetsChanges();
                        var persistIsSuccessfull = false;
                        try {
                            Handler.Persist(itemsToPersist, reason == PersistReason.Query);
                            persistIsSuccessfull = true;
                        }
                        catch (Exception) {
                            persistingIsFailed = true;
                            RollbackChangesOfEntitySets();
                            RestoreEntityChangesAfterPersistFailed();
                            throw;
                        }
                        finally {
                            if (persistIsSuccessfull || !Configuration.Supports(SessionOptions.NonTransactionalEntityStates))
                            {
                                DropDifferenceBackup();
                                foreach (var item in itemsToPersist.GetItems(PersistenceState.New))
                                {
                                    item.PersistenceState = PersistenceState.Synchronized;
                                }
                                foreach (var item in itemsToPersist.GetItems(PersistenceState.Modified))
                                {
                                    item.PersistenceState = PersistenceState.Synchronized;
                                }
                                foreach (var item in itemsToPersist.GetItems(PersistenceState.Removed))
                                {
                                    item.Update(null);
                                }

                                if (performPinning)
                                {
                                    EntityChangeRegistry = pinner.PinnedItems;
                                    pinner.Reset();
                                }
                                else
                                {
                                    EntityChangeRegistry.Clear();
                                }
                                EntitySetChangeRegistry.Clear();
                                NonPairedReferencesRegistry.Clear();
                            }
                            if (IsDebugEventLoggingEnabled)
                            {
                                OrmLog.Debug(Strings.LogSessionXPersistCompleted, this);
                            }
                        }
                    }
                    SystemEvents.NotifyPersisted();
                    Events.NotifyPersisted();
                }
                finally {
                    IsPersisting = false;
                }
            }
        }
Ejemplo n.º 3
0
 internal void Persist(PersistReason reason) => Persist(reason, false).GetAwaiter().GetResult();