{"record":{"id":"ece3dc7c555d4042","repo":"dotnet/efcore","slug":"cannot-translate-the-comparisonoperator-on-an-ece3dc","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.InMemory/Query/Internal/InMemoryExpressionTranslatingExpressionVisitor.cs","lineNumber":1268,"sourceCode":"        return expression is MethodCallExpression { Method.IsGenericMethod: true } readValueMethodCall\n            && readValueMethodCall.Method.GetGenericMethodDefinition() == ExpressionExtensions.ValueBufferTryReadValueMethod\n                ? readValueMethodCall.Arguments[2].GetConstantValue<IProperty>()\n                : null;\n    }\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        if (primaryKeyProperties == null)\n        {\n            throw new InvalidOperationException(\n                CoreStrings.EntityEqualityOnKeylessEntityNotSupported(\n                    nameof(Queryable.Contains), entityType.DisplayName()));\n        }\n\n        if (primaryKeyProperties.Count > 1)\n        {\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 ConstantExpression constantExpression:\n                var values = constantExpression.GetConstantValue<IEnumerable>();\n                var propertyValueList =","sourceCodeStart":1250,"sourceCodeEnd":1286,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.InMemory/Query/Internal/InMemoryExpressionTranslatingExpressionVisitor.cs#L1250-L1286","documentation":"Thrown from TryRewriteContainsEntity when an item passed to Queryable.Contains references an entity type that has no primary key (keyless). Without a key the translator cannot reduce entity equality to a key comparison, so it rejects the Contains.","triggerScenarios":"A query like db.MyView.Contains(entity) where MyView is configured HasNoKey() and the item is a StructuralTypeReferenceExpression whose StructuralType is a keyless IEntityType.","commonSituations":"Using Contains/ equality against a keyless entity (a view, a raw SQL result, or a query result without keys). Forgetting to define a primary key on an entity mapped to a database view.","solutions":["Configure a primary key on the entity (HasKey(...) or by convention) if the underlying rows are uniquely identifiable.","Rewrite the Contains to compare a primitive key property instead of the whole entity: db.Items.Select(i => i.Id).Contains(id).","If the type is genuinely keyless, perform the membership check client-side via AsEnumerable().","Re-evaluate whether the type should be keyless — most entities need a key."],"exampleFix":"// before\ndb.Views.Contains(viewEntity);\n// after - compare by a property\ndb.Views.Select(v => v.Code).Contains(viewEntity.Code);","handlingStrategy":"validation","validationCode":"var et = dbContext.Model.FindEntityType(typeof(MyView));\nif (et?.FindPrimaryKey() is null)\n    throw new InvalidOperationException(\"Cannot use Contains on keyless MyView; compare a property\");","typeGuard":"static bool IsKeyless(IEntityType et) => et.FindPrimaryKey() is null;","tryCatchPattern":"try { return db.Views.Contains(item); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"keyless entity\"))\n{\n    return db.Views.AsEnumerable().Contains(item);\n}","preventionTips":["Define a primary key on every entity that participates in Contains/equality.","Compare a single primitive key property instead of the whole entity.","Reserve HasNoKey for genuinely keyless query results."],"tags":["in-memory","query","translation","keyless-entity","entity-equality"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}