dotnet/efcore · error · InvalidOperationException

The number of value rows

Error message

The number of value rows ({valuesCount}) doesn't match the number of key rows ({keyCount}) for the data modification operation on '{table}'. Provide the same number of value rows and key rows.

What it means

Thrown when an UpdateData operation provides a different number of key rows vs value rows. EF requires KeyValues.GetLength(0) (number of rows to update) to equal Values.GetLength(0); every key row must have a corresponding value row.

Solutions

  1. Ensure both keyValues and values have the same number of rows (first dimension).
  2. If you only have one row to update, remove the extra rows from whichever array is longer.
  3. Re-scaffold the migration to regenerate both matrices from the model.

Example fix

// before
migrationBuilder.UpdateData(
    table: "Users",
    keyColumns: new[] { "Id" },
    keyValues: new object[,] { { 10 }, { 11 } }, // 2 rows
    columns: new[] { "Name" },
    values: new object[,] { { "Ann" } }); // 1 row

// after
migrationBuilder.UpdateData(
    table: "Users",
    keyColumns: new[] { "Id" },
    keyValues: new object[,] { { 10 }, { 11 } },
    columns: new[] { "Name" },
    values: new object[,] { { "Ann" }, { "Bob" } });
Defensive patterns

Strategy: validation

Validate before calling

static void CheckRowCounts(object[,] keyValues, object[,] values, string table)
{
    if (keyValues.GetLength(0) != values.GetLength(0))
        throw new ArgumentException($"{table}: keyValues row count must equal values row count");
}

Prevention

When it happens

Trigger: migrationBuilder.UpdateData(...) where the first dimension of keyValues (rows) differs from the first dimension of values (rows). E.g. two key rows but one value row.

Common situations: Batch-updating multiple rows and forgetting to extend one of the matrices. Copy-paste errors when adding a new row to only keyValues or only values.

Related errors


AI-assisted analysis of dotnet/efcore@3a2006ef56 (2026-08-11). Data as JSON: /api/errors/b8a23ed2b9a8593a. Report an issue: GitHub.

Appendix: source

Thrown at src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:1074

        IModel? model)
    {
        if (operation.KeyColumns.Length != operation.KeyValues.GetLength(1))
        {
            throw new InvalidOperationException(
                RelationalStrings.UpdateDataOperationKeyValuesCountMismatch(
                    operation.KeyValues.GetLength(1), operation.KeyColumns.Length, FormatTable(operation.Table, operation.Schema)));
        }

        if (operation.Columns.Length != operation.Values.GetLength(1))
        {
            throw new InvalidOperationException(
                RelationalStrings.UpdateDataOperationValuesCountMismatch(
                    operation.Values.GetLength(1), operation.Columns.Length, FormatTable(operation.Table, operation.Schema)));
        }

        if (operation.KeyValues.GetLength(0) != operation.Values.GetLength(0))
        {
            throw new InvalidOperationException(
                RelationalStrings.UpdateDataOperationRowCountMismatch(
                    operation.Values.GetLength(0), operation.KeyValues.GetLength(0), FormatTable(operation.Table, operation.Schema)));
        }

        if (operation.KeyColumnTypes != null
            && operation.KeyColumns.Length != operation.KeyColumnTypes.Length)
        {
            throw new InvalidOperationException(
                RelationalStrings.UpdateDataOperationKeyTypesCountMismatch(
                    operation.KeyColumnTypes.Length, operation.KeyColumns.Length, FormatTable(operation.Table, operation.Schema)));
        }

        if (operation.ColumnTypes != null
            && operation.Columns.Length != operation.ColumnTypes.Length)
        {
            throw new InvalidOperationException(
                RelationalStrings.UpdateDataOperationTypesCountMismatch(
                    operation.ColumnTypes.Length, operation.Columns.Length, FormatTable(operation.Table, operation.Schema)));

View on GitHub (pinned to 3a2006ef56)