{"record":{"id":"98d74aa965ff42d4","repo":"dotnet/efcore","slug":"the-entity-type-entitytype-does-not-have-a-pro","errorCode":null,"errorMessage":"The entity type '{entityType}' does not have a property mapped to the 'id' property in the database. Add a property mapped to 'id'.","messagePattern":"The entity type '(.+?)' does not have a property mapped to the 'id' property in the database\\. Add a property mapped to 'id'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":437,"sourceCode":"    ///     any release. You should only use it directly in your code with extreme caution and knowing that\n    ///     doing so can result in application failures when updating to a new Entity Framework Core release.\n    /// </summary>\n    protected virtual void ValidateKeys(\n        IEntityType entityType,\n        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)\n    {\n        var primaryKey = entityType.FindPrimaryKey();\n        if (primaryKey == null\n            || !entityType.IsDocumentRoot())\n        {\n            return;\n        }\n\n        var idProperty = entityType.GetProperties()\n            .FirstOrDefault(p => p.GetJsonPropertyName() == CosmosJsonIdConvention.IdPropertyJsonName);\n        if (idProperty == null)\n        {\n            throw new InvalidOperationException(CosmosStrings.NoIdProperty(entityType.DisplayName()));\n        }\n\n        var idType = idProperty.GetTypeMapping().Converter?.ProviderClrType\n            ?? idProperty.ClrType;\n        if (idType != typeof(string))\n        {\n            throw new InvalidOperationException(\n                CosmosStrings.IdNonStringStoreType(idProperty.Name, entityType.DisplayName(), idType.ShortDisplayName()));\n        }\n\n        var partitionKeyPropertyNames = entityType.GetPartitionKeyPropertyNames();\n        if (partitionKeyPropertyNames.Count == 0)\n        {\n            logger.NoPartitionKeyDefined(entityType);\n        }\n        else\n        {\n            if (entityType.BaseType != null","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L419-L455","documentation":"Thrown by ValidateKeys for a document-root entity type that has a primary key but no property whose JSON name resolves to \"id\" (CosmosJsonIdConvention.IdPropertyJsonName). Every Cosmos document requires an \"id\" property; the CosmosJsonIdConvention normally maps the PK to \"id\" automatically, so this fires only when that convention was disabled or overridden such that no property lands on the id JSON name.","triggerScenarios":"Explicitly renaming the primary key's JSON property away from \"id\" via ToJsonProperty(\"myId\") on a single-property string PK, or removing the id-mapped shadow property, on a root entity type.","commonSituations":"Custom id conventions that conflict with the built-in one; disabling CosmosJsonIdConvention; mapping a composite/non-string PK and then also suppressing the auto-generated id shadow property.","solutions":["Ensure exactly one property on the root entity is mapped to the JSON name \"id\", typically the single string primary key (modelBuilder.Entity<E>().Property(e => e.Id).ToJsonProperty(\"id\")).","Do not disable CosmosJsonIdConvention unless you provide your own id-mapped property.","If the PK is not a single string, allow the convention to generate the \"__id\"/\"id\" shadow property."],"exampleFix":"// before\nmodelBuilder.Entity<User>()\n    .Property(u => u.Id)\n    .ToJsonProperty(\"myId\"); // nothing mapped to \"id\"\n\n// after\nmodelBuilder.Entity<User>()\n    .Property(u => u.Id)\n    .ToJsonProperty(\"id\");","handlingStrategy":"validation","validationCode":"using var ctx = new MyContext();\nforeach (var e in ctx.Model.GetEntityTypes().Where(t => t.FindPrimaryKey() != null && t.IsDocumentRoot()))\n{\n    var idProp = e.GetProperties().FirstOrDefault(p => p.GetJsonPropertyName() == \"id\");\n    Debug.Assert(idProp is not null, $\"{e.Name} has no property mapped to the JSON 'id'\");\n}","typeGuard":null,"tryCatchPattern":"try { ctx.Model.GetModel(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"does not have a property mapped to the 'id'\"))\n{\n    logger.LogError(ex, \"Entity is missing a property mapped to Cosmos JSON 'id'\");\n    throw;\n}","preventionTips":["Keep a single string primary key and let the Cosmos convention map it to 'id'.","Do not disable CosmosJsonIdConvention without providing an explicit id-mapped property.","Test that every document-root entity has a property with JSON name 'id'."],"tags":["cosmos","configuration","model-validation","id","primary-key"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}