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 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);
        }