{"record":{"id":"9244b03da75a397b","repo":"dotnet/efcore","slug":"the-key-keyproperties-on-the-entity-type-entit","errorCode":null,"errorMessage":"The key {keyProperties} on the entity type '{entityType}' cannot be configured because the property '{property}' is contained in a complex type mapped to a JSON column. Keys cannot reference properties that are stored inside a JSON document.","messagePattern":"The key (.+?) on the entity type '(.+?)' cannot be configured because the property '(.+?)' is contained in a complex type mapped to a JSON column\\. Keys cannot reference properties that are stored inside a JSON document\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":228,"sourceCode":"        {\n            throw new InvalidOperationException(\n                RelationalStrings.AutoLoadedJsonProperty(property.Name, structuralType.DisplayName()));\n        }\n    }\n\n    /// <inheritdoc />\n    protected override void ValidateKey(\n        IKey key,\n        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)\n    {\n        base.ValidateKey(key, logger);\n\n        foreach (var property in key.Properties)\n        {\n            if (property.DeclaringType is IComplexType complexType\n                && complexType.IsMappedToJson())\n            {\n                throw new InvalidOperationException(\n                    RelationalStrings.KeyPropertyInJsonComplexType(\n                        key.Properties.Format(),\n                        key.DeclaringEntityType.DisplayName(),\n                        property.Name));\n            }\n        }\n\n        ValidateDefaultValuesOnKey(key, logger);\n        ValidateValueGeneration(key, logger);\n    }\n\n    /// <summary>\n    ///     Validates a primitive collection property.\n    /// </summary>\n    /// <param name=\"property\">The property to validate.</param>\n    /// <param name=\"logger\">The logger to use.</param>\n    protected override void ValidatePrimitiveCollection(\n        IProperty property,","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L210-L246","documentation":"ValidateKey (RelationalModelValidator.cs:223-234) throws when any property participating in a key is declared on a complex type that is mapped to a JSON column. Keys must be addressable as standalone columns (for indexing, FKs, joins), but properties inside a JSON document are embedded and cannot back a key, so the configuration is rejected.","triggerScenarios":"Declaring a key on an entity type that references a property which itself lives inside a JSON-mapped complex type (e.g. an owned JSON type exposes an Id that you tried to make part of the owner's key). Thrown during model validation.","commonSituations":"Trying to define a composite key that includes an owned JSON entity's Id; refactoring an owned type from table-splitting to JSON while a key reference remained; key conventions picking up a JSON-owned property.","solutions":["Move the key property out of the JSON-mapped complex type onto the owning entity as a real column.","If the owned type genuinely needs identity, stop mapping it to JSON and map it to its own table or as table-splitting columns.","Remove the key configuration that references the JSON-contained property and let EF treat the owned type as a value object."],"exampleFix":"// before\nmodelBuilder.Entity<Customer>().OwnsOne(c => c.Profile, p =>\n{\n    p.ToJson(\"ProfileJson\");\n});\nmodelBuilder.Entity<Customer>().HasKey(c => new { c.Id, c.Profile.ProfileId }); // ProfileId is in JSON -> throws\n\n// after - promote the key column out of JSON\nmodelBuilder.Entity<Customer>().Property(c => c.ProfileId).HasColumnName(\"ProfileId\");\nmodelBuilder.Entity<Customer>().HasKey(c => new { c.Id, c.ProfileId });","handlingStrategy":"validation","validationCode":"// Pre-validate: no key property may live in a JSON-mapped complex type.\nforeach (var et in context.Model.GetEntityTypes())\n{\n    foreach (var key in et.GetDeclaredKeys())\n    {\n        foreach (var p in key.Properties)\n        {\n            if (p.DeclaringType is IComplexType ct && ct.IsMappedToJson())\n            {\n                // will throw - move this property out of JSON.\n            }\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Keep key properties on the owning entity as real columns.","Don't reference JSON-contained properties in HasKey expressions.","When moving an owned type to JSON, audit existing key configurations."],"tags":["json-mapping","key","complex-type","model-validation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}