{"record":{"id":"5a61d876bc461b65","repo":"dotnet/efcore","slug":"the-number-of-values-valuescount-doesn-t-match","errorCode":null,"errorMessage":"The number of values ({valuesCount}) doesn't match the number of columns ({columnsCount}) for the data insertion operation on '{table}'. Provide the same number of values and columns.","messagePattern":"The number of values \\((.+?)\\) doesn't match the number of columns \\((.+?)\\) for the data insertion operation on '(.+?)'\\. Provide the same number of values and columns\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs","lineNumber":875,"sourceCode":"        if (terminate)\n        {\n            EndStatement(builder);\n        }\n    }\n\n    /// <summary>\n    ///     Generates the commands that correspond to the given operation.\n    /// </summary>\n    /// <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(","sourceCodeStart":857,"sourceCodeEnd":893,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs#L857-L893","documentation":"While generating commands for an InsertDataOperation, MigrationsSqlGenerator checks that the number of columns in operation.Columns equals the column dimension of the operation.Values 2D array. A mismatch means the data shape does not line up with the schema the insert targets, so EF refuses to emit potentially corrupt SQL.","triggerScenarios":"A migration calls migrationBuilder.InsertData(table, columns: new[] { \"A\",\"B\" }, values: new object[,] { { 1 }, { 2 } }) where the columns array length differs from values.GetLength(1). Often a hand-edited or programmatically generated migration with misaligned arrays.","commonSituations":"Manual edit of a migration file after adding/removing a column; values array built row-by-row but column list not updated; copy-paste from another table where the column count differed.","solutions":["Count the entries in columns and the inner dimension of values and make them equal.","Regenerate the migration with 'dotnet ef migrations add' so the scaffolder produces a consistent InsertData.","If inserting multiple rows, ensure each row has exactly one value per column."],"exampleFix":"// before — 2 columns declared, 1 value per row\nmigrationBuilder.InsertData(\n    table: \"Products\",\n    columns: new[] { \"Id\", \"Name\" },\n    values: new object[,] { { 1 }, { 2 } });\n\n// after\nmigrationBuilder.InsertData(\n    table: \"Products\",\n    columns: new[] { \"Id\", \"Name\" },\n    values: new object[,] { { 1, \"A\" }, { 2, \"B\" } });","handlingStrategy":"validation","validationCode":"// Validate an InsertDataOperation before it reaches the generator\nstatic void ValidateInsert(InsertDataOperation op)\n{\n    if (op.Columns.Length != op.Values.GetLength(1))\n        throw new ArgumentException(\n            $\"InsertData on {op.Table}: {op.Columns.Length} columns vs \" +\n            $\"{op.Values.GetLength(1)} value columns.\");\n}","typeGuard":"static bool InsertShapeOk(InsertDataOperation op)\n    => op.Columns.Length == op.Values.GetLength(1);","tryCatchPattern":null,"preventionTips":["Let the scaffolder generate InsertData rather than hand-writing it.","When editing a migration, recount columns and value arrays together.","Add a test that builds all migrations and runs them through the generator."],"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"}