{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Infrastructure/RelationalModelValidator.cs#L990-L1026","documentation":"Key property values must be immutable after the entity is inserted because they identify the row. The validator at line 1004-1009 throws when a key property has the ValueGenerated.OnUpdate flag set (via ValueGeneratedOnUpdate or ValueGeneratedOnAddOrUpdate) and is not an ordinal key property (used internally for some patterns). Allowing the database to change a key on update would break identity tracking and FK integrity.","triggerScenarios":"Calling `.ValueGeneratedOnUpdate()` or `.ValueGeneratedOnAddOrUpdate()` on a property that participates in the primary key (or any key). Also happens implicitly when a convention or provider applies these flags to a key column.","commonSituations":"Misconfiguring a primary key as `ValueGeneratedOnAddOrUpdate` instead of `ValueGeneratedOnAdd`; migrating a model where the key was previously non-generated; a custom convention setting OnUpdate on key properties.","solutions":["Use .ValueGeneratedOnAdd() instead of .ValueGeneratedOnAddOrUpdate()/.ValueGeneratedOnUpdate() for the key property.","If the key genuinely should change (rare), reconsider whether it is actually a key — restructure so the mutable column is a non-key property.","Remove explicit ValueGenerated configuration and let the default conventions (e.g. identity) apply."],"exampleFix":"// before\nmodelBuilder.Entity<Order>()\n    .Property(p => p.Id)\n    .ValueGeneratedOnAddOrUpdate(); // Id is the PK\n\n// after\nmodelBuilder.Entity<Order>()\n    .Property(p => p.Id)\n    .ValueGeneratedOnAdd();","handlingStrategy":"validation","validationCode":"foreach (var et in modelBuilder.Model.GetEntityTypes())\n{\n    var pk = et.FindPrimaryKey();\n    if (pk == null) continue;\n    foreach (var prop in pk.Properties)\n    {\n        if ((prop.ValueGenerated & ValueGenerated.OnUpdate) != 0\n            && !prop.IsOrdinalKeyProperty())\n        {\n            throw new InvalidOperationException(\n                $\"Key property '{prop.Name}' on {et.Name} must not be ValueGeneratedOnUpdate/AddOrUpdate.\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use ValueGeneratedOnAdd (not AddOrUpdate/OnUpdate) for identity/sequence key columns.","Audit any custom conventions that set ValueGenerated to ensure they skip key properties.","If a column must change on update, model it as a non-key property."],"tags":["ef-core","model-validation","keys","configuration"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}