/// <summary>
        /// The given save-update event named a transient entity.
        /// Here, we will perform the save processing.
        /// </summary>
        /// <param name="event">The save event to be handled. </param>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
        /// <returns> The entity's identifier after saving. </returns>
        protected virtual async Task <object> EntityIsTransientAsync(SaveOrUpdateEvent @event, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            log.Debug("saving transient instance");

            IEventSource source      = @event.Session;
            EntityEntry  entityEntry = @event.Entry;

            if (entityEntry != null)
            {
                if (entityEntry.Status == Status.Deleted)
                {
                    await(source.ForceFlushAsync(entityEntry, cancellationToken)).ConfigureAwait(false);
                }
                else
                {
                    throw new AssertionFailure("entity was persistent");
                }
            }

            object id = await(SaveWithGeneratedOrRequestedIdAsync(@event, cancellationToken)).ConfigureAwait(false);

            source.PersistenceContext.ReassociateProxy(@event.Entity, id);

            return(id);
        }
Example #2
0
        /// <summary>
        /// Prepares the save call by checking the session caches for a pre-existing
        /// entity and performing any lifecycle callbacks.
        /// </summary>
        /// <param name="entity">The entity to be saved. </param>
        /// <param name="id">The id by which to save the entity. </param>
        /// <param name="persister">The entity's persister instance. </param>
        /// <param name="useIdentityColumn">Is an identity column being used? </param>
        /// <param name="anything">Generally cascade-specific information. </param>
        /// <param name="source">The session from which the event originated. </param>
        /// <param name="requiresImmediateIdAccess">
        /// does the event context require
        /// access to the identifier immediately after execution of this method (if
        /// not, post-insert style id generators may be postponed if we are outside
        /// a transaction).
        /// </param>
        /// <param name="cancellationToken">A cancellation token that can be used to cancel the work</param>
        /// <returns>
        /// The id used to save the entity; may be null depending on the
        /// type of id generator used and the requiresImmediateIdAccess value
        /// </returns>
        protected virtual async Task <object> PerformSaveAsync(object entity, object id, IEntityPersister persister, bool useIdentityColumn, object anything, IEventSource source, bool requiresImmediateIdAccess, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (log.IsDebugEnabled())
            {
                log.Debug("saving {0}", MessageHelper.InfoString(persister, id, source.Factory));
            }

            EntityKey key;

            if (!useIdentityColumn)
            {
                key = source.GenerateEntityKey(id, persister);
                object old = source.PersistenceContext.GetEntity(key);
                if (old != null)
                {
                    if (source.PersistenceContext.GetEntry(old).Status == Status.Deleted)
                    {
                        await(source.ForceFlushAsync(source.PersistenceContext.GetEntry(old), cancellationToken)).ConfigureAwait(false);
                    }
                    else
                    {
                        throw new NonUniqueObjectException(id, persister.EntityName);
                    }
                }
                if (!(id is DelayedPostInsertIdentifier))
                {
                    persister.SetIdentifier(entity, id);
                }
            }
            else
            {
                key = null;
            }

            if (InvokeSaveLifecycle(entity, persister, source))
            {
                return(id);                //EARLY EXIT
            }
            return(await(PerformSaveOrReplicateAsync(entity, key, persister, useIdentityColumn, anything, source, requiresImmediateIdAccess, cancellationToken)).ConfigureAwait(false));
        }