{"record":{"id":"f7ba9f9ec4274544","repo":"dotnet/efcore","slug":"cannot-translate-comparisonoperator-on-a-subqu","errorCode":null,"errorMessage":"Cannot translate '{comparisonOperator}' on a subquery expression of entity type '{entityType}' because it has a composite primary key. See https://go.microsoft.com/fwlink/?linkid=2141942 for information on how to rewrite your query.","messagePattern":"Cannot translate '(.+?)' on a subquery expression of entity type '(.+?)' because it has a composite primary key\\. See https://go\\.microsoft\\.com/fwlink/\\?linkid=2141942 for information on how to rewrite your query\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.StructuralEquality.cs","lineNumber":42,"sourceCode":"    {\n        result = null;\n\n        if (item is not StructuralTypeReferenceExpression { StructuralType: IEntityType entityType })\n        {\n            return false;\n        }\n\n        var primaryKeyProperties = entityType.FindPrimaryKey()?.Properties;\n\n        switch (primaryKeyProperties)\n        {\n            case null:\n                throw new InvalidOperationException(\n                    CoreStrings.EntityEqualityOnKeylessEntityNotSupported(\n                        nameof(Queryable.Contains), entityType.DisplayName()));\n\n            case { Count: > 1 }:\n                throw new InvalidOperationException(\n                    CoreStrings.EntityEqualityOnCompositeKeyEntitySubqueryNotSupported(\n                        nameof(Queryable.Contains), entityType.DisplayName()));\n        }\n\n        var property = primaryKeyProperties[0];\n        Expression rewrittenSource;\n        switch (source)\n        {\n            case SqlConstantExpression sqlConstantExpression:\n                var values = (IEnumerable)sqlConstantExpression.Value!;\n                var propertyValueList =\n                    (IList)Activator.CreateInstance(typeof(List<>).MakeGenericType(property.ClrType.MakeNullable()))!;\n                var propertyGetter = property.GetGetter();\n                foreach (var value in values)\n                {\n                    propertyValueList.Add(propertyGetter.GetClrValue(value));\n                }\n","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.StructuralEquality.cs#L24-L60","documentation":"Thrown by TryRewriteContainsEntity when a Contains operates on a subquery whose entity type has a composite primary key (more than one key property). The Cosmos provider's Contains rewrite extracts a single key property into a list; with a composite key it cannot produce a valid single-value Contains, so it throws.","triggerScenarios":"Calling Contains on the result of a subquery returning entities with composite keys, e.g. subquery.Where(...).Contains(entity) where the entity has multiple PK properties.","commonSituations":"Entities modeled with composite keys used in Contains against a subquery. Relational patterns ported to Cosmos where composite-key Contains was supported differently.","solutions":["Rewrite the Contains as an Any with a predicate comparing all composite key properties: subquery.Any(x => x.K1 == e.K1 && x.K2 == e.K2).","Project a single discriminant value (e.g. a concatenated id) from the subquery and Contains on that.","Materialize the subquery results client-side and compare composite keys in memory."],"exampleFix":"// before\nvar exists = await context.Items.Where(...).Contains(item).AnyAsync();\n// after\nvar exists = await context.Items.Where(...)\n    .AnyAsync(x => x.K1 == item.K1 && x.K2 == item.K2);","handlingStrategy":"validation","validationCode":"// For composite keys, use Any with a multi-property predicate\nvar exists = await subquery.AnyAsync(x => x.K1 == item.K1 && x.K2 == item.K2);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Avoid Contains over subqueries returning composite-key entities.","Rewrite as Any comparing all composite key properties.","Project a single discriminant value when Contains semantics are required."],"tags":["cosmos","composite-key","entity-equality","contains","subquery","translation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}