{"record":{"id":"4ab0a146d41a03bd","repo":"dotnet/efcore","slug":"the-number-of-key-values-valuescount-doesn-t-m-4ab0a1","errorCode":null,"errorMessage":"The number of key values ({valuesCount}) doesn't match the number of key columns ({columnsCount}) for the data modification operation on '{table}'. Provide the same number of key values and key columns.","messagePattern":"The number of key values \\((.+?)\\) doesn't match the number of key columns \\((.+?)\\) for the data modification operation on '(.+?)'\\. Provide the same number of key values and key columns\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs","lineNumber":1060,"sourceCode":"        }\n\n        builder.Append(sqlBuilder.ToString());\n        EndStatement(builder);\n    }\n\n    /// <summary>\n    ///     Generates the commands that correspond to the given operation.\n    /// </summary>\n    /// <param name=\"operation\">The data operation to generate commands for.</param>\n    /// <param name=\"model\">The model.</param>\n    /// <returns>The commands that correspond to the given operation.</returns>\n    protected virtual IEnumerable<IReadOnlyModificationCommand> GenerateModificationCommands(\n        UpdateDataOperation operation,\n        IModel? model)\n    {\n        if (operation.KeyColumns.Length != operation.KeyValues.GetLength(1))\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.UpdateDataOperationKeyValuesCountMismatch(\n                    operation.KeyValues.GetLength(1), operation.KeyColumns.Length, FormatTable(operation.Table, operation.Schema)));\n        }\n\n        if (operation.Columns.Length != operation.Values.GetLength(1))\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.UpdateDataOperationValuesCountMismatch(\n                    operation.Values.GetLength(1), operation.Columns.Length, FormatTable(operation.Table, operation.Schema)));\n        }\n\n        if (operation.KeyValues.GetLength(0) != operation.Values.GetLength(0))\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.UpdateDataOperationRowCountMismatch(\n                    operation.Values.GetLength(0), operation.KeyValues.GetLength(0), FormatTable(operation.Table, operation.Schema)));\n        }\n","sourceCodeStart":1042,"sourceCodeEnd":1078,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs#L1042-L1078","documentation":"Thrown inside GenerateModificationCommands(UpdateDataOperation, ...) in src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:1060 (message key UpdateDataOperationKeyValuesCountMismatch). It compares operation.KeyColumns.Length against operation.KeyValues.GetLength(1) and throws InvalidOperationException when they differ. UpdateData builds a WHERE clause per row from the key columns, so the per-row key value count must equal the key column count.","triggerScenarios":"A migration calls migrationBuilder.UpdateData(keyColumns: new[] { \"Id\", \"TenantId\" }, keyValues: new object[,] { { 1 } }, columns: new[] { \"Name\" }, values: new object[,] { { \"x\" } }) where the inner dimension of keyValues does not equal keyColumns.Length. Common after editing a generated migration for a composite-key entity.","commonSituations":"Hand-editing UpdateData calls for composite-key tables and miscounting key values. Modifying HasData seed rows for composite-key entities and then editing the scaffolded migration. Copy-paste of UpdateData blocks.","solutions":["Ensure every row of keyValues has exactly operation.KeyColumns.Length entries (keyValues.GetLength(1) == keyColumns.Length).","Re-scaffold the migration so UpdateData key arrays match the current composite key.","Manage seed data through HasData so EF scaffolds UpdateData consistently.","Audit: assert keyValues.GetLength(1) == keyColumns.Length for each UpdateData."],"exampleFix":"// before (composite key 2 columns, 1 key value):\nmigrationBuilder.UpdateData(\n    table: \"Members\",\n    keyColumns: new[] { \"UserId\", \"GroupId\" },\n    keyValues: new object[,] { { 7 } },\n    columns: new[] { \"Role\" },\n    values: new object[,] { { \"Admin\" } });\n\n// after (one key value per key column):\nmigrationBuilder.UpdateData(\n    table: \"Members\",\n    keyColumns: new[] { \"UserId\", \"GroupId\" },\n    keyValues: new object[,] { { 7, 3 } },\n    columns: new[] { \"Role\" },\n    values: new object[,] { { \"Admin\" } });","handlingStrategy":"validation","validationCode":"static bool UpdateKeyShapeIsValid(string[] keyColumns, object[,] keyValues)\n{\n    return keyValues.GetLength(1) == keyColumns.Length;\n}\n\n// usage before calling migrationBuilder.UpdateData\nif (!UpdateKeyShapeIsValid(keyColumns, keyValues))\n    throw new InvalidOperationException(\"keyValues second dimension must equal keyColumns.Length\");","typeGuard":null,"tryCatchPattern":"try\n{\n    await dbContext.Database.MigrateAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"number of key values\") && ex.Message.Contains(\"data modification\"))\n{\n    logger.LogError(ex, \"An UpdateDataOperation has a keyValues/keyColumns length mismatch; fix the migration.\");\n    throw;\n}","preventionTips":["Prefer HasData management so EF scaffolds UpdateData with correct composite-key arrays.","When editing UpdateData for composite keys, update keyColumns and every keyValues row together.","Add a test asserting keyValues.GetLength(1) == keyColumns.Length for each UpdateData.","Re-scaffold migrations after key changes rather than hand-editing."],"tags":["migrations","data-seeding","update-data","validation","invalidoperation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}