{"record":{"id":"193f9645298e6829","repo":"dotnet/efcore","slug":"the-number-of-column-types-typescount-doesn-t","errorCode":null,"errorMessage":"The number of column types ({typesCount}) doesn't match the number of columns ({columnsCount}) for the data insertion 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 insertion 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":884,"sourceCode":"    /// <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        InsertDataOperation operation,\n        IModel? model)\n    {\n        if (operation.Columns.Length != operation.Values.GetLength(1))\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.InsertDataOperationValuesCountMismatch(\n                    operation.Values.GetLength(1), operation.Columns.Length,\n                    FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));\n        }\n\n        if (operation.ColumnTypes != null\n            && operation.Columns.Length != operation.ColumnTypes.Length)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.InsertDataOperationTypesCountMismatch(\n                    operation.ColumnTypes.Length, operation.Columns.Length,\n                    FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));\n        }\n\n        if (operation.ColumnTypes == null\n            && model == null)\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.InsertDataOperationNoModel(\n                    FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));\n        }\n\n        var propertyMappings = operation.ColumnTypes == null\n            ? GetPropertyMappings(operation.Columns, operation.Table, operation.Schema, model)\n            : null;\n\n        for (var i = 0; i < operation.Values.GetLength(0); i++)","sourceCodeStart":866,"sourceCodeEnd":902,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs#L866-L902","documentation":"Thrown inside GenerateModificationCommands(InsertDataOperation, ...) in src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:884 (message key InsertDataOperationTypesCountMismatch). It throws InvalidOperationException when operation.ColumnTypes is non-null but its length differs from operation.Columns.Length. ColumnTypes is an optional explicit type override array; if you supply it, it must line up element-for-element with Columns.","triggerScenarios":"A migration calls migrationBuilder.InsertData with the columnTypes: parameter set to an array whose length differs from columns:. For example columns: new[] { \"A\", \"B\" } with columnTypes: new[] { \"int\" }. Common after editing an auto-generated migration and removing a column type without removing the column.","commonSituations":"Manually adding columnTypes to an InsertData and miscounting. Editing a migration to remove a column but forgetting to remove the corresponding columnType entry. Using model-less migrations where types must be specified by hand.","solutions":["Make operation.ColumnTypes either null (so types are resolved from the model) or an array with exactly operation.Columns.Length elements.","Re-scaffold the migration to regenerate consistent columnTypes arrays.","Add the entity to the model (modelBuilder.Entity<T>()) and use HasData so column types are inferred and columnTypes can be omitted.","Audit the migration so that for every i, Columns[i] pairs with ColumnTypes[i]."],"exampleFix":"// before (2 columns but 1 column type):\nmigrationBuilder.InsertData(\n    table: \"Users\",\n    columns: new[] { \"Id\", \"Name\" },\n    columnTypes: new[] { \"int\" },\n    values: new object[,] { { 1, \"Alice\" } });\n\n// after (aligned):\nmigrationBuilder.InsertData(\n    table: \"Users\",\n    columns: new[] { \"Id\", \"Name\" },\n    columnTypes: new[] { \"int\", \"nvarchar(100)\" },\n    values: new object[,] { { 1, \"Alice\" } });","handlingStrategy":"validation","validationCode":"static bool InsertTypesAreValid(string[] columns, string[]? columnTypes)\n{\n    return columnTypes == null || columnTypes.Length == columns.Length;\n}\n\n// usage\nif (!InsertTypesAreValid(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 insertion\"))\n{\n    logger.LogError(ex, \"An InsertDataOperation has a columnTypes/columns length mismatch; fix the migration.\");\n    throw;\n}","preventionTips":["Omit columnTypes entirely when an entity maps the table (types come from the model).","When supplying columnTypes, keep them in the same order and count as columns.","Re-scaffold migrations after column changes rather than hand-editing type arrays.","Add a test asserting columnTypes == null || columnTypes.Length == columns.Length."],"tags":["migrations","data-seeding","insert-data","validation","invalidoperation"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}