{"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/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs#L866-L902","documentation":"For an InsertDataOperation where ColumnTypes is provided, EF checks that ColumnTypes.Length equals Columns.Length. Column types let the generator emit typed SQL when no model is available; an uneven list would leave some columns untyped or misaligned, so EF rejects it.","triggerScenarios":"migrationBuilder.InsertData(..., columns: new[] { \"A\",\"B\" }, columnTypes: new[] { \"int\" }, ...) — the columnTypes array is shorter/longer than columns. Common in hand-written migrations or when a column was added without updating the types array.","commonSituations":"Editing a migration file by hand; programmatically generating InsertData where types and columns come from different sources; cross-provider migration where types were specified explicitly.","solutions":["Provide exactly one column type per entry in columns, in the same order.","Regenerate the migration so EF emits consistent columns/columnTypes.","Drop columnTypes entirely if a model is attached (EF infers types), which also avoids this check."],"exampleFix":"// before\nmigrationBuilder.InsertData(\n    table: \"Products\",\n    columns: new[] { \"Id\", \"Name\" },\n    columnTypes: new[] { \"int\" },\n    values: new object[,] { { 1, \"A\" } });\n\n// after\nmigrationBuilder.InsertData(\n    table: \"Products\",\n    columns: new[] { \"Id\", \"Name\" },\n    columnTypes: new[] { \"int\", \"nvarchar(100)\" },\n    values: new object[,] { { 1, \"A\" } });","handlingStrategy":"validation","validationCode":"// Validate column types align with columns for InsertData\nstatic void ValidateInsertTypes(InsertDataOperation op)\n{\n    if (op.ColumnTypes is { } types && types.Length != op.Columns.Length)\n        throw new ArgumentException(\n            $\"InsertData on {op.Table}: {types.Length} types vs \" +\n            $\"{op.Columns.Length} columns.\");\n}","typeGuard":"static bool InsertTypesOk(InsertDataOperation op)\n    => op.ColumnTypes is null || op.ColumnTypes.Length == op.Columns.Length;","tryCatchPattern":null,"preventionTips":["Either always supply columnTypes (one per column) or omit them and rely on the model.","Regenerate migrations after structural changes so types stay aligned.","Lint migration files in CI for InsertData shape consistency."],"tags":["migrations","sql-generation","data-integrity","seeding"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}