{"record":{"id":"593ec50b73b23266","repo":"dotnet/efcore","slug":"the-number-of-values-valuescount-doesn-t-match-593ec5","errorCode":null,"errorMessage":"The number of values ({valuesCount}) doesn't match the number of columns ({columnsCount}) for the data modification 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 modification 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":1067,"sourceCode":"    ///     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        UpdateDataOperation operation,\n        IModel? model)\n    {\n        if (operation.KeyColumns.Length != operation.KeyValues.GetLength(1))\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.UpdateDataOperationKeyValuesCountMismatch(\n                    operation.KeyValues.GetLength(1), operation.KeyColumns.Length, FormatTable(operation.Table, operation.Schema)));\n        }\n\n        if (operation.Columns.Length != operation.Values.GetLength(1))\n        {\n            throw new InvalidOperationException(\n                RelationalStrings.UpdateDataOperationValuesCountMismatch(\n                    operation.Values.GetLength(1), operation.Columns.Length, FormatTable(operation.Table, operation.Schema)));\n        }\n\n        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        }","sourceCodeStart":1049,"sourceCodeEnd":1085,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs#L1049-L1085","documentation":"Thrown inside GenerateModificationCommands(UpdateDataOperation, ...) in src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:1067 (message key UpdateDataOperationValuesCountMismatch). It compares operation.Columns.Length against operation.Values.GetLength(1) and throws InvalidOperationException when they differ. The SET clause assigns one value per column, so each value row must have exactly Columns.Length entries.","triggerScenarios":"A migration calls migrationBuilder.UpdateData(columns: new[] { \"A\", \"B\" }, values: new object[,] { { 1 } }, ...) where the inner dimension of values does not equal columns.Length. Happens after editing a generated migration and dropping a value, or scaffolding HasData changes and hand-editing.","commonSituations":"Editing generated UpdateData and miscounting the values array. Changing the set of updated columns without updating values. Copy-paste of UpdateData blocks across entities.","solutions":["Ensure every row of values has exactly operation.Columns.Length entries (values.GetLength(1) == columns.Length).","Re-scaffold the migration to regenerate consistent values arrays.","Drive seed changes through HasData so columns and values stay in sync.","Audit: assert values.GetLength(1) == columns.Length for each UpdateData."],"exampleFix":"// before (2 columns, 1 value):\nmigrationBuilder.UpdateData(\n    table: \"Users\",\n    keyColumns: new[] { \"Id\" },\n    keyValues: new object[,] { { 1 } },\n    columns: new[] { \"Name\", \"Email\" },\n    values: new object[,] { { \"Alice\" } });\n\n// after (aligned):\nmigrationBuilder.UpdateData(\n    table: \"Users\",\n    keyColumns: new[] { \"Id\" },\n    keyValues: new object[,] { { 1 } },\n    columns: new[] { \"Name\", \"Email\" },\n    values: new object[,] { { \"Alice\", \"a@x.com\" } });","handlingStrategy":"validation","validationCode":"static bool UpdateValueShapeIsValid(string[] columns, object[,] values)\n{\n    return values.GetLength(1) == columns.Length;\n}\n\n// usage before calling migrationBuilder.UpdateData\nif (!UpdateValueShapeIsValid(columns, 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 modification\"))\n{\n    logger.LogError(ex, \"An UpdateDataOperation has a values/columns length mismatch; fix the migration.\");\n    throw;\n}","preventionTips":["Prefer HasData management so EF scaffolds UpdateData consistently.","When editing UpdateData, update columns and every values row together.","Add a test asserting values.GetLength(1) == columns.Length for each UpdateData.","Re-scaffold migrations after column changes rather than hand-editing."],"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"}