Repository: dotnet/efcore
File: src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.cs (C#)

Code before:
                CreatePropertyAccessExpression(item, property)));

        return true;
    }

    private bool TryRewriteEntityEquality(
        ExpressionType nodeType,
        Expression left,
        Expression right,
        bool equalsMethod,
        [NotNullWhen(true)] out Expression? result)
    {
        var entityReference = left as EntityReferenceExpression ?? right as EntityReferenceExpression;
        if (entityReference == null)
        {
            result = null;
            return false;
        }

        var entityType = entityReference.EntityType;
        var compareReference = entityReference == left ? right : left;

        // Null equality
        if (IsNullSqlConstantExpression(compareReference))
        {
            if (entityType.IsDocumentRoot() && entityReference.Subquery == null)
            {
                // Document root can never be null
                result = Visit(Expression.Constant(nodeType != ExpressionType.Equal));
                return true;
            }

            // Treat type as object for null comparison
            var access = new SqlObjectAccessExpression(entityReference.Object);
            result = sqlExpressionFactory.MakeBinary(
                nodeType,
                access,
                sqlExpressionFactory.Constant(null, typeof(object))!,
                typeMappingSource.FindMapping(typeof(bool)))!;
            return true;
        }

        if (entityType.FindPrimaryKey()?.Properties is not { } primaryKeyProperties)
        {
            throw new InvalidOperationException(
                CoreStrings.EntityEqualityOnKeylessEntityNotSupported(
                    nodeType == ExpressionType.Equal
                        ? equalsMethod ? nameof(object.Equals) : "=="
                        : equalsMethod
                            ? "!" + nameof(object.Equals)
                            : "!=",

Review comment (suggestion):
While it's true that document root can never be null, why distinguish between document root non-roots which are also non-nullable, rather than having a single code path handling both? Concretely, you could simply check `entityReference.Parameter.IsNullable` - this should be false for both roots and non-nullable non-roots.

Code after:
                EnumerableMethods.Contains.MakeGenericMethod(property.ClrType.MakeNullable()),
                rewrittenSource,
                CreatePropertyAccessExpression(item, property)));

        return true;
    }

    private bool TryRewriteEntityEquality(
        ExpressionType nodeType,
        Expression left,
        Expression right,
        bool equalsMethod,
        [NotNullWhen(true)] out Expression? result)
    {
        var leftEntityReference = left as EntityReferenceExpression;
        var rightEntityReference = right as EntityReferenceExpression;

        if (leftEntityReference == null
            && rightEntityReference == null)
        {
            result = null;
            return false;
        }

        if (left is SqlConstantExpression { Value: null }
            || right is SqlConstantExpression { Value: null })
        {
            var nonNullEntityReference = (left is SqlConstantExpression { Value: null } ? rightEntityReference : leftEntityReference)!;
            var shaper = nonNullEntityReference.Parameter
                ?? (StructuralTypeShaperExpression)nonNullEntityReference.Subquery!.ShaperExpression;

            if (!shaper.IsNullable)
            {
                result = Visit(Expression.Constant(nodeType != ExpressionType.Equal));
                return true;
            }

            var access = Visit(shaper.ValueBufferExpression);
            result = new SqlBinaryExpression(
                nodeType,
                access,
                sqlExpressionFactory.Constant(null, typeof(object), CosmosTypeMapping.Default)!,
                typeof(bool),
                typeMappingSource.FindMapping(typeof(bool)))!;
            return true;
        }

        var leftEntityType = leftEntityReference?.EntityType;
        var rightEntityType = rightEntityReference?.EntityType;
        var entityType = leftEntityType ?? rightEntityType;