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