{"record":{"id":"02aef052e7244e5a","repo":"dotnet/efcore","slug":"cannot-translate-the-comparisonoperator-on-an","errorCode":null,"errorMessage":"Cannot translate the '{comparisonOperator}' on an expression of entity type '{entityType}' because it is a keyless entity. Consider using entity properties instead. For more information on keyless entity types, see https://go.microsoft.com/fwlink/?linkid=2141943.","messagePattern":"Cannot translate the '(.+?)' on an expression of entity type '(.+?)' because it is a keyless entity\\. Consider using entity properties instead\\. For more information on keyless entity types, see https://go\\.microsoft\\.com/fwlink/\\?linkid=2141943\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.StructuralEquality.cs","lineNumber":37,"sourceCode":"\n    private static readonly MethodInfo ParameterListValueExtractorMethod =\n        typeof(CosmosSqlTranslatingExpressionVisitor).GetTypeInfo().GetDeclaredMethod(nameof(ParameterListValueExtractor))!;\n\n    private bool TryRewriteContainsEntity(Expression source, Expression item, [NotNullWhen(true)] out Expression? result)\n    {\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();","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.StructuralEquality.cs#L19-L55","documentation":"Thrown by TryRewriteContainsEntity when a Queryable.Contains call (or entity equality) targets a keyless entity type. Keyless entities have no primary key, so EF cannot rewrite an entity-based Contains/equality into a key comparison for Cosmos SQL. Contains on keyless entities is not translatable.","triggerScenarios":"Calling .Contains(entity) or comparing entity instances where the entity type is configured HasNoKey(). For example: context.Views.Contains(view) where View is a keyless query root.","commonSituations":"Using keyless entity types (database views, raw SQL results, read-only projections) in Contains or entity-equality comparisons. Migrating a relational Contains pattern to Cosmos with a keyless type.","solutions":["Compare on a concrete property of the keyless entity instead of the entity instance: ids.Contains(v.SomeId).","If the entity should be identifiable, define a key on it (remove HasNoKey) so entity equality can translate.","Materialize to a set of scalar keys client-side and use Contains on those scalars."],"exampleFix":"// before\nvar view = context.Views.First();\nvar exists = context.Views.Contains(view);\n// after\nvar viewId = context.Views.Select(v => v.Code).First();\nvar exists = context.Views.Any(v => v.Code == viewId);","handlingStrategy":"validation","validationCode":"// Compare on scalar keys, not on keyless entity references\nvar codes = views.Select(v => v.Code).ToArray();\nvar exists = context.Views.Any(v => codes.Contains(v.Code));","typeGuard":"static bool IsKeyless(IEntityType et) => et.FindPrimaryKey() is null;","tryCatchPattern":null,"preventionTips":["Never use Contains/entity-equality on keyless entity instances.","Compare scalar properties of keyless entities instead.","Add a key if entity identity comparison is genuinely needed."],"tags":["cosmos","keyless","entity-equality","contains","translation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}