{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L419-L455","documentation":"Thrown when a document-root entity type has a primary key but no property mapped to the Cosmos JSON 'id' property. Cosmos requires every document to have a string 'id' field, so EF Core requires each root entity to expose one (often the PK converted to string, or a shadow id). The validator scans GetProperties() for one whose GetJsonPropertyName() equals the well-known id JSON name and throws if none exists.","triggerScenarios":"Defining an entity with HasPrimaryKey but never mapping a property to 'id' - e.g. configuring an int PK without HasConversion to string and without the conventional id-mapping shim, or removing the default id mapping. Fires during model validation for any document-root entity.","commonSituations":"Customizing the primary key without ensuring a string 'id' mapping; setting ToProperty(\"id\") incorrectly; turning off conventions that auto-create the id shadow property; new entity type that the conventions did not wire up an id for.","solutions":["Ensure at least one property is mapped to the JSON 'id' name - typically the PK with a string conversion: modelBuilder.Entity<T>().HasKey(...).HasConversion<string>() or rely on the built-in id convention.","If you want the PK itself to serve as id, configure its JSON property name to 'id' (GetJsonPropertyName) and ensure a string converter.","Call HasShadowId on the root if you want EF to manage a shadow id property.","Verify you have not disabled the CosmosJsonIdConvention or removed the id mapping in OnModelCreating."],"exampleFix":"// before\nmodelBuilder.Entity<Thing>()\n    .ToContainer(\"Things\")\n    .HasKey(t => t.Number);   // int PK, no string 'id'\n\n// after\nmodelBuilder.Entity<Thing>()\n    .ToContainer(\"Things\")\n    .HasKey(t => t.Number);\nmodelBuilder.Entity<Thing>().Property(t => t.Number).HasConversion<string>().ToJsonProperty(\"id\");","handlingStrategy":"validation","validationCode":"foreach (var et in dbContext.Model.GetEntityTypes().Where(e => e.IsDocumentRoot()))\n{\n    var hasId = et.GetProperties().Any(p => p.GetJsonPropertyName() == CosmosJsonIdConvention.IdPropertyJsonName);\n    if (!hasId) throw new InvalidOperationException($\"{et.DisplayName()} missing 'id' mapping\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep the CosmosJsonIdConvention enabled so an id shadow property is created automatically.","When customizing the PK, always ensure one property maps to 'id' as a string.","Add a unit test that builds the model for every root entity."],"tags":["cosmos","model-validation","id","primary-key","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}