{"record":{"id":"c394428143b2bd3b","repo":"dotnet/efcore","slug":"the-number-of-key-column-types-typescount-does-c39442","errorCode":null,"errorMessage":"The number of key column types ({typesCount}) doesn't match the number of key columns ({columnsCount}) for the data modification operation on '{table}'. Provide the same number of key column types and key columns.","messagePattern":"The number of key column types \\((.+?)\\) doesn't match the number of key columns \\((.+?)\\) for the data modification operation on '(.+?)'\\. Provide the same number of key column types and key columns\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs","lineNumber":1082,"sourceCode":"\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\n        if (operation.KeyColumnTypes != null\n            && operation.KeyColumns.Length != operation.KeyColumnTypes.Length)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.UpdateDataOperationKeyTypesCountMismatch(\n                    operation.KeyColumnTypes.Length, operation.KeyColumns.Length, FormatTable(operation.Table, operation.Schema)));\n        }\n\n        if (operation.ColumnTypes != null\n            && operation.Columns.Length != operation.ColumnTypes.Length)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.UpdateDataOperationTypesCountMismatch(\n                    operation.ColumnTypes.Length, operation.Columns.Length, FormatTable(operation.Table, operation.Schema)));\n        }\n\n        if (operation.KeyColumnTypes == null\n            && model == null)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.UpdateDataOperationNoModel(\n                    FormatTable(operation.Table, operation.Schema)));","sourceCodeStart":1064,"sourceCodeEnd":1100,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs#L1064-L1100","documentation":"Thrown inside GenerateModificationCommands(UpdateDataOperation, ...) in src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:1082 (message key UpdateDataOperationKeyTypesCountMismatch). It throws InvalidOperationException when operation.KeyColumnTypes is non-null but its length differs from operation.KeyColumns.Length. KeyColumnTypes is an optional type override for the WHERE-clause key columns; if supplied it must align element-for-element with KeyColumns.","triggerScenarios":"A migration calls migrationBuilder.UpdateData with keyColumnTypes: set to an array whose length differs from keyColumns:. For example keyColumns: new[] { \"Id\", \"TenantId\" } with keyColumnTypes: new[] { \"int\" }. Common after editing a migration and removing a key column without removing its type entry.","commonSituations":"Manually adding keyColumnTypes and miscounting. Editing a model-less migration and forgetting a key type entry. Refactoring a composite key without updating the type array.","solutions":["Make operation.KeyColumnTypes either null (types resolved from the model) or an array of exactly operation.KeyColumns.Length.","Re-scaffold the migration to regenerate consistent keyColumnTypes.","Map the table to an entity type so keyColumnTypes can be omitted.","Audit: for every i, KeyColumns[i] must pair with KeyColumnTypes[i]."],"exampleFix":"// before (2 key columns, 1 key type):\nmigrationBuilder.UpdateData(\n    table: \"Members\",\n    keyColumns: new[] { \"UserId\", \"GroupId\" },\n    keyColumnTypes: new[] { \"int\" },\n    keyValues: new object[,] { { 7, 3 } },\n    columns: new[] { \"Role\" },\n    values: new object[,] { { \"Admin\" } });\n\n// after (aligned):\nmigrationBuilder.UpdateData(\n    table: \"Members\",\n    keyColumns: new[] { \"UserId\", \"GroupId\" },\n    keyColumnTypes: new[] { \"int\", \"int\" },\n    keyValues: new object[,] { { 7, 3 } },\n    columns: new[] { \"Role\" },\n    values: new object[,] { { \"Admin\" } });","handlingStrategy":"validation","validationCode":"static bool UpdateKeyTypesAreValid(string[] keyColumns, string[]? keyColumnTypes)\n{\n    return keyColumnTypes == null || keyColumnTypes.Length == keyColumns.Length;\n}\n\n// usage\nif (!UpdateKeyTypesAreValid(keyColumns, keyColumnTypes))\n    throw new InvalidOperationException(\"keyColumnTypes.Length must equal keyColumns.Length (or be null)\");","typeGuard":null,"tryCatchPattern":"try\n{\n    await dbContext.Database.MigrateAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"number of key column types\") && ex.Message.Contains(\"data modification\"))\n{\n    logger.LogError(ex, \"An UpdateDataOperation has a keyColumnTypes/keyColumns length mismatch; fix the migration.\");\n    throw;\n}","preventionTips":["Omit keyColumnTypes when an entity maps the table.","When supplying keyColumnTypes, keep them in the same order and count as keyColumns.","Re-scaffold migrations after key changes rather than hand-editing.","Add a test asserting keyColumnTypes == null || keyColumnTypes.Length == keyColumns.Length."],"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"}