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

Code before:
        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 be null
                result = Visit(Expression.Constant(nodeType != ExpressionType.Equal));
                return true;
            }

            var isNull = sqlExpressionFactory.Function("IS_NULL", [entityReference.Object], typeof(bool));
            var isDefined = sqlExpressionFactory.Function("IS_DEFINED", [entityReference.Object], typeof(bool));
            var notDefined = sqlExpressionFactory.Not(isDefined);
            var check = sqlExpressionFactory.MakeBinary(ExpressionType.OrElse, isNull, notDefined, typeMappingSource.FindMapping(typeof(bool))) ?? throw new UnreachableException();

            if (nodeType == ExpressionType.NotEqual)
            {
                check = sqlExpressionFactory.Not(check);
            }

            result = check;
            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)
                        : "!=",
                entityType.DisplayName()));
        }

        if (compareReference is EntityReferenceExpression compareEntityReference)
        {
            // Comparing of 2 different entity types is always false.
            if (entityType.GetRootType() != compareEntityReference.EntityType.GetRootType())
            {
                result = Visit(Expression.Constant(false));
                return true;
            }
        }

        result = Visit(
            primaryKeyProperties.Select(p =>

Review comment (suggestion):
Entity equality/null comparison no longer throws for keyless entity types: the keyless check is skipped in the null-comparison branch and only enforced for non-null comparisons. Other providers still throw for `entity == null` when the entity type is keyless (e.g. RelationalSqlTranslatingExpressionVisitor.StructuralEquality.cs:146-157). If this isn’t intentionally Cosmos-specific, consider preserving the keyless exception behavior here as well to avoid inconsistent semantics across providers.

Code after:
        {
            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;

        Check.DebugAssert(entityType != null, "At least either side should be entityReference so entityType should be non-null.");

        if (leftEntityType != null
            && rightEntityType != null
            && leftEntityType.GetRootType() != rightEntityType.GetRootType())
        {
            result = sqlExpressionFactory.Constant(false);
            return true;
        }

        var primaryKeyProperties = entityType.FindPrimaryKey()?.Properties;
        if (primaryKeyProperties == null)
        {
            throw new InvalidOperationException(
                CoreStrings.EntityEqualityOnKeylessEntityNotSupported(
                    nodeType == ExpressionType.Equal
                        ? equalsMethod ? nameof(object.Equals) : "=="
                        : equalsMethod
                            ? "!" + nameof(object.Equals)