dotnet/efcore · error · InvalidOperationException
The number of values
Error message
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. What it means
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.
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.
Example fix
// before — 2 columns declared, 1 value per row
migrationBuilder.InsertData(
table: "Products",
columns: new[] { "Id", "Name" },
values: new object[,] { { 1 }, { 2 } });
// after
migrationBuilder.InsertData(
table: "Products",
columns: new[] { "Id", "Name" },
values: new object[,] { { 1, "A" }, { 2, "B" } }); Defensive patterns
Strategy: validation
Validate before calling
// Validate an InsertDataOperation before it reaches the generator
static void ValidateInsert(InsertDataOperation op)
{
if (op.Columns.Length != op.Values.GetLength(1))
throw new ArgumentException(
$"InsertData on {op.Table}: {op.Columns.Length} columns vs " +
$"{op.Values.GetLength(1)} value columns.");
} Type guard
static bool InsertShapeOk(InsertDataOperation op)
=> op.Columns.Length == op.Values.GetLength(1); Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The data insertion operation on
- The number of column types
- The number of key values
- A seed entity for entity type
- A seed entity for entity type
AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11).
Data as JSON: /api/errors/5a61d876bc461b65.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:875
if (terminate)
{
EndStatement(builder);
}
}
/// <summary>
/// Generates the commands that correspond to the given operation.
/// </summary>
/// <param name="operation">The data operation to generate commands for.</param>
/// <param name="model">The model.</param>
/// <returns>The commands that correspond to the given operation.</returns>
protected virtual IEnumerable<IReadOnlyModificationCommand> GenerateModificationCommands(
InsertDataOperation operation,
IModel? model)
{
if (operation.Columns.Length != operation.Values.GetLength(1))
{
throw new InvalidOperationException(
RelationalStrings.InsertDataOperationValuesCountMismatch(
operation.Values.GetLength(1), operation.Columns.Length,
FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));
}
if (operation.ColumnTypes != null
&& operation.Columns.Length != operation.ColumnTypes.Length)
{
throw new InvalidOperationException(
RelationalStrings.InsertDataOperationTypesCountMismatch(
operation.ColumnTypes.Length, operation.Columns.Length,
FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));
}
if (operation.ColumnTypes == null
&& model == null)
{
throw new InvalidOperationException(View on GitHub (pinned to 3a2006ef56)