{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs#L1064-L1100","documentation":"Thrown when an UpdateData operation supplies KeyColumnTypes whose length differs from KeyColumns. EF needs a one-to-one mapping so it can resolve a type mapping per key parameter.","triggerScenarios":"migrationBuilder.UpdateData(...) with keyColumnTypes: of wrong length. Checked after the row-count check in GenerateModificationCommands(UpdateDataOperation).","commonSituations":"Hand-edited composite-key update where a key column was added but keyColumnTypes was not updated. SQLite migrations that explicitly declare key types.","solutions":["Make keyColumnTypes length equal to keyColumns length, one store type per key column.","Drop keyColumnTypes entirely to fall back on the model.","Re-scaffold the migration so types are written consistently."],"exampleFix":"// before\nmigrationBuilder.UpdateData(\n    table: \"Users\",\n    keyColumns: new[] { \"TenantId\", \"Id\" },\n    keyColumnTypes: new[] { \"int\" }, // length 1 != 2\n    keyValues: new object[,] { { 1, 10 } },\n    columns: new[] { \"Name\" },\n    values: new object[,] { { \"Ann\" } });\n\n// after\nmigrationBuilder.UpdateData(\n    table: \"Users\",\n    keyColumns: new[] { \"TenantId\", \"Id\" },\n    keyColumnTypes: new[] { \"int\", \"int\" },\n    keyValues: new object[,] { { 1, 10 } },\n    columns: new[] { \"Name\" },\n    values: new object[,] { { \"Ann\" } });","handlingStrategy":"validation","validationCode":"static void CheckKeyTypeCount(string[] keyColumns, string[]? keyColumnTypes, string table)\n{\n    if (keyColumnTypes is not null && keyColumnTypes.Length != keyColumns.Length)\n        throw new ArgumentException($\"{table}: keyColumnTypes length must equal keyColumns length\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive keyColumnTypes from keyColumns whenever explicit types are needed.","Prefer model-driven type resolution and omit keyColumnTypes.","Re-scaffold to keep arrays in sync."],"tags":["ef-core","migrations","updatedata","argument-validation"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}