private void ApplyForeignKeyChanges <TRelatedEntity>(EntityRelationship relationship, IEnumerable <TEntity> entities)
        {
            var entityMapper = new EntityMapper <TRelatedEntity>();
            var defaultId    = entityMapper.GetDefaultId();

            foreach (var entity in entities)
            {
                var relatedEntity = (TRelatedEntity)relationship.NavigationProperty.GetValue(entity);
                var entityId      = relatedEntity == null ? defaultId : entityMapper.GetIdValue(relatedEntity);
                relationship.IdProperty.SetValue(entity, entityId);
            }
        }
#pragma warning disable CRR0026 // Unused member - used via Reflection
        private void CommitRelationship <TRelatedEntity>(EntityRelationship relationship, IEnumerable <TEntity> entities)
        {
            var collection = BuildRelatedEntityCollection <TRelatedEntity>(relationship, entities);

            if (collection.Any())
            {
                var dbSet = new MongoDbSet <TRelatedEntity>();
                dbSet.SetDatabase(Database);
                dbSet.AddRange(collection);
                dbSet.SaveChanges();
            }

            if (!relationship.IsCollection)
            {
                ApplyForeignKeyChanges <TRelatedEntity>(relationship, entities);
            }
        }
#pragma warning restore CRR0026 // Unused member - used via Reflection

#pragma warning disable CRR0026 // Unused member - used via Reflection
        private async Task CommitRelationshipAsync <TRelatedEntity>(EntityRelationship relationship, IEnumerable <TEntity> entities, CancellationToken cancellationToken)
        {
            var collection = BuildRelatedEntityCollection <TRelatedEntity>(relationship, entities);

            cancellationToken.ThrowIfCancellationRequested();

            if (collection.Any())
            {
                var dbSet = new MongoDbSet <TRelatedEntity>();
                dbSet.SetDatabase(Database);
                dbSet.AddRange(collection);
                await dbSet.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
            }

            if (!relationship.IsCollection)
            {
                ApplyForeignKeyChanges <TRelatedEntity>(relationship, entities);
            }
        }
#pragma warning restore CRR0026 // Unused member - used via Reflection

        private IDbEntityCollection <TRelatedEntity> BuildRelatedEntityCollection <TRelatedEntity>(EntityRelationship relationship, IEnumerable <TEntity> entities)
        {
            var collection   = new DbEntityCollection <TRelatedEntity>();
            var entityMapper = new EntityMapper <TRelatedEntity>();

            var defaultId = entityMapper.GetDefaultId();
            var relatedEntitiesToProcess = new List <TRelatedEntity>();

            foreach (var entity in entities)
            {
                if (relationship.IsCollection)
                {
                    if (relationship.NavigationProperty.GetValue(entity) is ICollection <TRelatedEntity> navigationCollection)
                    {
                        relatedEntitiesToProcess.AddRange(navigationCollection);
                    }
                }
                else
                {
                    var relatedEntity = (TRelatedEntity)relationship.NavigationProperty.GetValue(entity);
                    if (relatedEntity != null)
                    {
                        relatedEntitiesToProcess.Add(relatedEntity);
                    }
                }
            }

            foreach (var relatedEntity in relatedEntitiesToProcess)
            {
                var entityId = entityMapper.GetIdValue(relatedEntity);
                if (Equals(entityId, defaultId))
                {
                    collection.Add(relatedEntity);
                }
            }

            return(collection);
        }