{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosSqlTranslatingExpressionVisitor.StructuralEquality.cs#L19-L55","documentation":"When translating Queryable.Contains on an entity, StructuralEquality needs the entity's single primary key property to build an IN clause. If FindPrimaryKey returns null (keyless entity) it throws InvalidOperationException CoreStrings.EntityEqualityOnKeylessEntityNotSupported — Contains cannot be expressed without a key.","triggerScenarios":"Calling .Contains(entity) or building an IN-style filter where the element type is a keyless entity (HasNoKey configured).","commonSituations":"Modeling a view/report type as keyless and then attempting Contains/Any-equality filters against it, or accidentally removing a key configuration.","solutions":["Compare on a concrete property instead of the entity: .Where(x => ids.Contains(x.SomeId)).","Configure a key on the entity if equality semantics are genuinely needed.","Materialize the keyless sequence client-side and filter there."],"exampleFix":"// before\nvar q = db.Views.Where(v => allowed.Contains(v));\n// after\nvar q = db.Views.Where(v => allowedIds.Contains(v.Code));","handlingStrategy":"validation","validationCode":"// compare on scalar keys, not on the entity\nstatic IQueryable<T> InByKey<T>(IQueryable<T> q, IEnumerable<string> keys, string propName)\n    where T : class\n    => q.Where(x => keys.Contains(EF.Property<string>(x, propName)));","typeGuard":"static bool EntityHasKey<T>(DbContext ctx)\n    => ctx.Model.FindEntityType(typeof(T))?.FindPrimaryKey() is not null;","tryCatchPattern":null,"preventionTips":["Avoid Contains on entity instances; project to a key first.","Add a key to any entity used in equality filters.","Test Contains behavior whenever a key is removed."],"tags":["cosmos","entity-equality","keyless","query-translation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}