{"record":{"id":"5e571b2e11d0712d","repo":"dotnet/efcore","slug":"the-property-keyproperty-cannot-be-configured","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.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs","lineNumber":500,"sourceCode":"                }\n            }\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    ///     This is an internal API that supports the Entity Framework Core infrastructure and not subject to\n    ///     the same compatibility standards as public APIs. It may be changed or removed without notice in\n    ///     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 ValidateDatabaseProperties(\n        IEntityType entityType,\n        IDiagnosticsLogger<DbLoggerCategory.Model.Validation> logger)\n    {\n        var properties = new Dictionary<string, IPropertyBase>();\n        foreach (var property in entityType.GetProperties().Where(x => x.IsPersisted()))\n        {\n            var jsonName = property.GetJsonPropertyName();\n","sourceCodeStart":482,"sourceCodeEnd":518,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L482-L518","documentation":"Thrown when a key property is configured with a ValueGenerated strategy that includes the OnUpdate flag (ValueGeneratedOnUpdate or ValueGeneratedOnAddOrUpdate). Key values must be immutable after the entity is stored, so generating a new value on update would mutate the key and break identity tracking. The Cosmos validator overrides ValidateMutableKey and rejects any such property unless it is an ordinal key property (an internal exception).","triggerScenarios":"Calling .ValueGeneratedOnAddOrUpdate() or .ValueGeneratedOnUpdate() on a property that is part of a primary key or alternate key. Fires during key validation at model finalization.","commonSituations":"Slapping a generic audit-field convention (e.g. 'always regenerate on save') onto every property including keys; copy-pasting a row-version/concurrency configuration onto a key column; misusing the ValueGenerated API to mimic a database default.","solutions":["Remove the ValueGeneratedOnUpdate / ValueGeneratedOnAddOrUpdate call from the key property.","If the key should be database-generated, use ValueGenerated.OnAdd (generated once at insert, never updated).","Move the auto-updating behavior to a non-key property (e.g. a RowVersion or UpdatedAt)."],"exampleFix":"// before\nmodelBuilder.Entity<Order>()\n    .Property(o => o.OrderId)\n    .ValueGeneratedOnAddOrUpdate(); // OrderId is the PK\n\n// after\nmodelBuilder.Entity<Order>()\n    .Property(o => o.OrderId)\n    .ValueGeneratedOnAdd();","handlingStrategy":"validation","validationCode":"foreach (var et in dbContext.Model.GetEntityTypes())\n{\n    foreach (var key in et.GetKeys())\n    {\n        foreach (var p in key.Properties)\n        {\n            if (p.ValueGenerated.HasFlag(ValueGenerated.OnUpdate))\n                throw new InvalidOperationException($\"{p.Name} is a mutable key property\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never apply ValueGeneratedOnUpdate / ValueGeneratedOnAddOrUpdate to key properties.","Use ValueGenerated.OnAdd for insert-only generated keys.","Keep auto-update conventions scoped to non-key audit fields."],"tags":["cosmos","model-validation","key","value-generated","configuration"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}