{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Cosmos/Infrastructure/Internal/CosmosModelValidator.cs#L482-L518","documentation":"Thrown by ValidateMutableKey when a property that participates in a key has ValueGenerated with the OnUpdate flag set (ValueGeneratedOnUpdate or ValueGeneratedOnAddOrUpdate), unless it is an ordinal key property. Key values are immutable once the entity is stored, so EF forbids configuring a key property as update-generated. Note the validator throws CoreStrings.MutableKeyProperty (shared with the core provider).","triggerScenarios":"Calling .ValueGeneratedOnUpdate() or .ValueGeneratedOnAddOrUpdate() on a property that is part of a primary key or alternate key.","commonSituations":"Reusing a relational convention (e.g. row version / update-generated timestamp) on a key column; applying a default value-generation strategy to all properties including keys; copying configuration from a non-key property.","solutions":["Remove ValueGeneratedOnUpdate/ValueGeneratedOnAddOrUpdate from the key property; use ValueGenerated.Never or ValueGenerated.OnAdd if a store-generated key is intended.","Move the update-generated behavior to a non-key property (e.g. an ETag concurrency token mapped to _etag).","If you need an auto-updating key-like value, use a separate non-key column."],"exampleFix":"// before\nmodelBuilder.Entity<Doc>()\n    .Property(d => d.Id)\n    .ValueGeneratedOnAddOrUpdate();\n\n// after\nmodelBuilder.Entity<Doc>()\n    .Property(d => d.Id)\n    .ValueGeneratedOnAdd();","handlingStrategy":"validation","validationCode":"using var ctx = new MyContext();\nforeach (var e in ctx.Model.GetEntityTypes())\n{\n    foreach (var key in e.GetDeclaredKeys())\n    {\n        foreach (var p in key.Properties)\n        {\n            Debug.Assert(!p.ValueGenerated.HasFlag(Microsoft.EntityFrameworkCore.Metadata.ValueGenerated.OnUpdate),\n                $\"{e.Name}.{p.Name} is a key property and must not be ValueGeneratedOnUpdate/AddOrUpdate\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try { ctx.Model.GetModel(); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"cannot be configured as 'ValueGeneratedOnUpdate'\"))\n{\n    logger.LogError(ex, \"A key property was configured as update-generated\");\n    throw;\n}","preventionTips":["Never apply ValueGeneratedOnUpdate/ValueGeneratedOnAddOrUpdate to key properties.","Reserve update-generated columns for non-key fields like ETag/_etag.","Add a test scanning all keys for the OnUpdate ValueGenerated flag."],"tags":["cosmos","configuration","model-validation","key","value-generated"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}