internal SecurityEntity CreateEntitySafe(int entityId, int parentEntityId, int ownerId, bool?isInherited = null, bool?hasExplicitEntry = null)
        {
            SecurityEntity parent = null;

            if (parentEntityId != default)
            {
                // if the parent cannot be loaded (even from the db), this will throw an exception
                parent = GetEntitySafe(parentEntityId, true);
            }

            var entity = new SecurityEntity
            {
                Id          = entityId,
                IsInherited = isInherited ?? true,
                OwnerId     = ownerId,
                Parent      = parent
            };

            parent?.AddChild(entity);
            _cache.Entities[entityId] = entity;

            return(entity);
        }