{"record":{"id":"ed25476e25319e00","repo":"dotnet/efcore","slug":"the-number-of-key-values-valuescount-doesn-t-m","errorCode":null,"errorMessage":"The number of key values ({valuesCount}) doesn't match the number of key columns ({columnsCount}) for the data deletion 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 deletion 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":969,"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        DeleteDataOperation operation,\n        IModel? model)\n    {\n        if (operation.KeyColumns.Length != operation.KeyValues.GetLength(1))\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.DeleteDataOperationValuesCountMismatch(\n                    operation.KeyValues.GetLength(1), operation.KeyColumns.Length, FormatTable(operation.Table, operation.Schema)));\n        }\n\n        if (operation.KeyColumnTypes != null\n            && operation.KeyColumns.Length != operation.KeyColumnTypes.Length)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.DeleteDataOperationTypesCountMismatch(\n                    operation.KeyColumnTypes.Length, operation.KeyColumns.Length, FormatTable(operation.Table, operation.Schema)));\n        }\n\n        if (operation.KeyColumnTypes == null\n            && model == null)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.DeleteDataOperationNoModel(\n                    FormatTable(operation.Table, operation.Schema)));","sourceCodeStart":951,"sourceCodeEnd":987,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs#L951-L987","documentation":"Thrown inside GenerateModificationCommands(DeleteDataOperation, ...) in src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:969 (message key DeleteDataOperationValuesCountMismatch). It compares operation.KeyColumns.Length against operation.KeyValues.GetLength(1) and throws InvalidOperationException when they differ. DeleteData 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.DeleteData(keyColumns: new[] { \"Id\", \"TenantId\" }, keyValues: new object[,] { { 1 } }) where the inner dimension of keyValues does not match the keyColumns length. Happens after editing a generated migration (e.g. composite key) and dropping a key value, or scaffolding HasData removal for a composite-key entity incorrectly.","commonSituations":"Editing generated DeleteData calls for composite-key tables and miscounting. Removing a HasData seed row for a composite-key entity and then hand-editing the migration. Copy-paste errors in hand-authored migrations.","solutions":["Ensure every row in keyValues has exactly operation.KeyColumns.Length entries (the second dimension of the 2D array equals the keyColumns array length).","Re-scaffold the migration so DeleteData arrays are regenerated for the current composite key.","Use modelBuilder.Entity<T>().HasData(...) and remove rows there, letting EF scaffold the DeleteData consistently.","Audit: assert keyValues.GetLength(1) == keyColumns.Length for each DeleteData."],"exampleFix":"// before (composite key 2 columns, only 1 value):\nmigrationBuilder.DeleteData(\n    table: \"Members\",\n    keyColumns: new[] { \"UserId\", \"GroupId\" },\n    keyValues: new object[,] { { 7 } });\n\n// after (one value per key column):\nmigrationBuilder.DeleteData(\n    table: \"Members\",\n    keyColumns: new[] { \"UserId\", \"GroupId\" },\n    keyValues: new object[,] { { 7, 3 } });","handlingStrategy":"validation","validationCode":"static bool DeleteDataKeyShapeIsValid(string[] keyColumns, object[,] keyValues)\n{\n    return keyValues.GetLength(1) == keyColumns.Length;\n}\n\n// usage before calling migrationBuilder.DeleteData\nif (!DeleteDataKeyShapeIsValid(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 deletion\"))\n{\n    logger.LogError(ex, \"A DeleteDataOperation has a keyValues/keyColumns length mismatch; fix the migration.\");\n    throw;\n}","preventionTips":["Prefer HasData management so EF scaffolds DeleteData with correct composite-key arrays.","When editing DeleteData for composite keys, update keyColumns and every keyValues row together.","Add a test asserting keyValues.GetLength(1) == keyColumns.Length for each DeleteData.","Re-scaffold migrations after key changes rather than hand-editing."],"tags":["migrations","data-seeding","delete-data","validation","invalidoperation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}