/// <summary> Adds an entity to the internal caches.</summary>
        public static EntityEntry AddEntity(
            this IPersistenceContext context,
            object entity,
            Status status,
            object[] loadedState,
            EntityKey entityKey,
            object version,
            LockMode lockMode,
            bool existsInDatabase,
            IEntityPersister persister,
            bool disableVersionIncrement)
        {
            if (context is StatefulPersistenceContext statefulPersistence)
            {
                return(statefulPersistence.AddEntity(
                           entity,
                           status,
                           loadedState,
                           entityKey,
                           version,
                           lockMode,
                           existsInDatabase,
                           persister,
                           disableVersionIncrement));
            }

#pragma warning disable 618
            return(context.AddEntity(
                       entity,
                       status,
                       loadedState,
                       entityKey,
                       version,
                       lockMode,
                       existsInDatabase,
                       persister,
                       disableVersionIncrement,
                       loadedState?.Any(o => o == LazyPropertyInitializer.UnfetchedProperty) == true));

#pragma warning restore 618
        }
        public virtual void OnDelete(DeleteEvent @event, ISet <object> transientEntities)
        {
            IEventSource        source             = @event.Session;
            IPersistenceContext persistenceContext = source.PersistenceContext;
            object entity = persistenceContext.UnproxyAndReassociate(@event.Entity);

            EntityEntry      entityEntry = persistenceContext.GetEntry(entity);
            IEntityPersister persister;
            object           id;
            object           version;

            if (entityEntry == null)
            {
                log.Debug("entity was not persistent in delete processing");

                persister = source.GetEntityPersister(@event.EntityName, entity);

                if (ForeignKeys.IsTransientSlow(persister.EntityName, entity, source))
                {
                    DeleteTransientEntity(source, entity, @event.CascadeDeleteEnabled, persister, transientEntities);
                    // EARLY EXIT!!!
                    return;
                }
                else
                {
                    PerformDetachedEntityDeletionCheck(@event);
                }

                id = persister.GetIdentifier(entity);

                if (id == null)
                {
                    throw new TransientObjectException("the detached instance passed to delete() had a null identifier");
                }

                EntityKey key = source.GenerateEntityKey(id, persister);

                persistenceContext.CheckUniqueness(key, entity);

                new OnUpdateVisitor(source, id, entity).Process(entity, persister);

                version = persister.GetVersion(entity);

                entityEntry = persistenceContext.AddEntity(
                    entity,
                    persister.IsMutable ? Status.Loaded : Status.ReadOnly,
                    persister.GetPropertyValues(entity),
                    key,
                    version,
                    LockMode.None,
                    true,
                    persister,
                    false);
            }
            else
            {
                log.Debug("deleting a persistent instance");

                if (entityEntry.Status == Status.Deleted || entityEntry.Status == Status.Gone)
                {
                    log.Debug("object was already deleted");
                    return;
                }
                persister = entityEntry.Persister;
                id        = entityEntry.Id;
                version   = entityEntry.Version;
            }

            if (InvokeDeleteLifecycle(source, entity, persister))
            {
                return;
            }

            DeleteEntity(source, entity, entityEntry, @event.CascadeDeleteEnabled, persister, transientEntities);

            if (source.Factory.Settings.IsIdentifierRollbackEnabled)
            {
                persister.ResetIdentifier(entity, id, version);
            }
        }