{"record":{"id":"79407bd87962f3f4","repo":"dotnet/efcore","slug":"the-property-keyproperty-cannot-be-configured-79407b","errorCode":null,"errorMessage":"The property '{keyProperty}' cannot be configured as 'ValueGeneratedOnUpdate' or 'ValueGeneratedOnAddOrUpdate' because it's part of a key and its value cannot be changed after the entity has been added to the store.","messagePattern":"The property '(.+?)' cannot be configured as 'ValueGeneratedOnUpdate' or 'ValueGeneratedOnAddOrUpdate' because it's part of a key and its value cannot be changed after the entity has been added to the store\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs","lineNumber":1008,"sourceCode":"        {\n            logger.ModelValidationKeyDefaultValueWarning(propertyWithDefault);\n        }\n    }\n\n    /// <summary>\n    ///     Validates that a key doesn't have mutable properties.\n    /// </summary>\n    /// <param name=\"key\">The key to validate.</param>\n    /// <param name=\"logger\">The logger to use.</param>\n    protected override void ValidateMutableKey(\n        IKey key,\n        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)\n    {\n        var mutableProperty = key.Properties.FirstOrDefault(p => p.ValueGenerated.HasFlag(ValueGenerated.OnUpdate));\n        if (mutableProperty != null\n            && !mutableProperty.IsOrdinalKeyProperty())\n        {\n            throw new InvalidOperationException(CoreStrings.MutableKeyProperty(mutableProperty.Name));\n        }\n    }\n\n    /// <summary>\n    ///     Validates a single table and all entity types mapped to it.\n    /// </summary>\n    /// <param name=\"mappedTypes\">The entity types mapped to the table.</param>\n    /// <param name=\"table\">The table identifier.</param>\n    /// <param name=\"logger\">The logger to use.</param>\n    protected virtual void ValidateTable(\n        IReadOnlyList<IEntityType> mappedTypes,\n        in StoreObjectIdentifier table,\n        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)\n    {\n        var nonJsonMappedTypes = mappedTypes.Where(e => !e.IsMappedToJson()).ToList();\n        if (nonJsonMappedTypes.Count > 0)\n        {\n            ValidateSharedTableCompatibility(nonJsonMappedTypes, table, logger);","sourceCodeStart":990,"sourceCodeEnd":1026,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L990-L1026","documentation":"Thrown by ValidateMutableKey when a property participating in a primary/alternate key has ValueGenerated.OnUpdate (ValueGeneratedOnUpdate or ValueGeneratedOnAddOrUpdate) and is not an ordinal key property (the rare TPT/sequence-style exception). Key values must be immutable once the entity exists; allowing the store to regenerate them on update would break identity tracking.","triggerScenarios":"builder.Property(p => p.Id).ValueGeneratedOnAddOrUpdate() (or OnUpdate) where p => Id participates in a key. The validator scans key.Properties for any ValueGenerated.HasFlag(ValueGenerated.OnUpdate) and throws unless IsOrdinalKeyProperty() returns true.","commonSituations":"Misconfiguring an identity column as ValueGeneratedOnAddOrUpdate thinking that helps with updates; copying a generated-column config onto a key; provider convention that accidentally marks a key column with OnUpdate.","solutions":["Use ValueGeneratedOnAdd (not OnAddOrUpdate/OnUpdate) for key columns generated by the store on insert: builder.Property(p => p.Id).ValueGeneratedOnAdd().","If the property must not be regenerated, set ValueGenerated.Never.","Verify the key composition — if the offending property is unnecessarily part of the key, remove it from the key."],"exampleFix":"// before\nbuilder.Property(o => o.OrderId)\n    .ValueGeneratedOnAddOrUpdate(); // OrderId is the PK\nbuilder.HasKey(o => o.OrderId);\n\n// after\nbuilder.Property(o => o.OrderId)\n    .ValueGeneratedOnAdd();\nbuilder.HasKey(o => o.OrderId);","handlingStrategy":"validation","validationCode":"foreach (var et in model.GetEntityTypes())\n{\n    foreach (var key in et.GetKeys())\n    {\n        foreach (var prop in key.Properties)\n        {\n            if (prop.ValueGenerated.HasFlag(Microsoft.EntityFrameworkCore.Metadata.ValueGenerated.OnUpdate)\n                && !prop.IsOrdinalKeyProperty())\n            {\n                Console.WriteLine($\"Key property {et.Name}.{prop.Name} is ValueGenerated.OnUpdate; use ValueGeneratedOnAdd.\");\n            }\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default key columns to ValueGeneratedOnAdd; reserve OnUpdate for non-key columns.","Review key composition whenever you set ValueGeneratedOnAddOrUpdate on a column.","Add a model-validation test that catches this in CI."],"tags":["ef-core","model-validation","keys","value-generation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}