dotnet/efcore · error · InvalidOperationException

The number of column types

Error message

The number of column types ({typesCount}) doesn't match the number of columns ({columnsCount}) for the data modification operation on '{table}'. Provide the same number of column types and columns.

What it means

Thrown when an UpdateData operation supplies ColumnTypes whose length differs from Columns. Each updated column must declare its store type when explicit types are in use.

Solutions

  1. Resize columnTypes so its length equals columns length.
  2. Remove columnTypes to let the model resolve store types.
  3. Re-scaffold the migration to produce consistent arrays.

Example fix

// before
migrationBuilder.UpdateData(
    table: "Users",
    keyColumns: new[] { "Id" },
    keyValues: new object[,] { { 10 } },
    columns: new[] { "FirstName", "LastName" },
    columnTypes: new[] { "nvarchar(50)" }, // 1 type, 2 columns
    values: new object[,] { { "Ann", "Smith" } });

// after
migrationBuilder.UpdateData(
    table: "Users",
    keyColumns: new[] { "Id" },
    keyValues: new object[,] { { 10 } },
    columns: new[] { "FirstName", "LastName" },
    columnTypes: new[] { "nvarchar(50)", "nvarchar(50)" },
    values: new object[,] { { "Ann", "Smith" } });
Defensive patterns

Strategy: validation

Validate before calling

static void CheckColumnTypeCount(string[] columns, string[]? columnTypes, string table)
{
    if (columnTypes is not null && columnTypes.Length != columns.Length)
        throw new ArgumentException($"{table}: columnTypes length must equal columns length");
}

Prevention

When it happens

Trigger: migrationBuilder.UpdateData(...) with columnTypes: array of wrong length. Checked in GenerateModificationCommands(UpdateDataOperation) right after the key-types check.

Common situations: Hand-edited update where a target column was added without a matching type entry. Migrations targeting providers that need explicit types (SQLite).

Related errors


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

Appendix: source

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

        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)));
        }

        if (operation.KeyColumnTypes == null
            && model == null)
        {
            throw new InvalidOperationException(
                RelationalStrings.UpdateDataOperationNoModel(
                    FormatTable(operation.Table, operation.Schema)));
        }

        var keyPropertyMappings = operation.KeyColumnTypes == null
            ? GetPropertyMappings(operation.KeyColumns, operation.Table, operation.Schema, model)
            : null;
        var propertyMappings = operation.ColumnTypes == null
            ? GetPropertyMappings(operation.Columns, operation.Table, operation.Schema, model)
            : null;

View on GitHub (pinned to 3a2006ef56)