{"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/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs#L857-L893","documentation":"Thrown inside GenerateModificationCommands(InsertDataOperation, ...) in src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:875 (message key InsertDataOperationValuesCountMismatch). It compares operation.Columns.Length against operation.Values.GetLength(1) (the column dimension of the 2D values array) and throws InvalidOperationException when they differ. The migration API for InsertData takes a rectangular object[,] whose second dimension must equal the number of columns.","triggerScenarios":"A hand-authored (or hand-edited) migration calls migrationBuilder.InsertData(columns: new[] { \"A\", \"B\" }, values: new object[,] { { 1, 2, 3 } }) where the inner dimension of values does not match the columns array. Also when seed-data scaffolding produces a malformed values array, or when a developer edits an auto-generated migration and forgets a value.","commonSituations":"Editing a generated migration's InsertData values array and miscounting. Building a migration programmatically with mismatched column/value arrays. Copy-pasting InsertData calls and forgetting to add/remove a corresponding column. Model seed data (HasData) that resolves to a bad array after a column rename.","solutions":["Ensure every row in the values 2D array has exactly operation.Columns.Length entries (the second dimension matches the columns array length).","Re-scaffold the migration (dotnet ef migrations add) from the corrected model so InsertData arrays are regenerated consistently.","Prefer modelBuilder.Entity<T>().HasData(...) for seed data over hand-authoring InsertData, so EF keeps columns and values in sync.","Run a quick local build/script that asserts values.GetLength(1) == columns.Length for every InsertData in the migration."],"exampleFix":"// before (mismatch: 2 columns, 3 values per row):\nmigrationBuilder.InsertData(\n    table: \"Users\",\n    columns: new[] { \"Id\", \"Name\" },\n    values: new object[,] { { 1, \"Alice\", true } });\n\n// after (aligned: 2 columns, 2 values per row):\nmigrationBuilder.InsertData(\n    table: \"Users\",\n    columns: new[] { \"Id\", \"Name\" },\n    values: new object[,] { { 1, \"Alice\" } });","handlingStrategy":"validation","validationCode":"static bool InsertDataShapeIsValid(string[] columns, object[,] values)\n{\n    return values.GetLength(1) == columns.Length;\n}\n\n// usage before calling migrationBuilder.InsertData\nif (!InsertDataShapeIsValid(new[] { \"Id\", \"Name\" }, values))\n    throw new InvalidOperationException(\"values second dimension must equal columns.Length\");","typeGuard":null,"tryCatchPattern":"try\n{\n    await dbContext.Database.MigrateAsync();\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"number of values\") && ex.Message.Contains(\"data insertion\"))\n{\n    logger.LogError(ex, \"An InsertDataOperation has a values/columns length mismatch; fix the migration.\");\n    throw;\n}","preventionTips":["Prefer modelBuilder.Entity<T>().HasData(...) over hand-authored InsertData so EF keeps arrays consistent.","When editing a generated InsertData, always update both columns and every value row together.","Add a unit test that scans migration files and asserts values.GetLength(1) == columns.Length.","Re-scaffold migrations after model changes rather than hand-editing values arrays."],"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"}