dotnet/efcore · error · InvalidOperationException

The number of column types ({typesCount}) doesn't match the

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 inside GenerateModificationCommands(UpdateDataOperation, ...) in src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:1090 (message key UpdateDataOperationTypesCountMismatch). It throws InvalidOperationException when operation.ColumnTypes is non-null but its length differs from operation.Columns.Length. ColumnTypes is an optional type override for the SET-clause columns; if supplied it must align element-for-element with Columns.

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 dbf9771522)

Solutions

  1. Make operation.ColumnTypes either null (types resolved from the model) or an array of exactly operation.Columns.Length.
  2. Re-scaffold the migration to regenerate consistent columnTypes.
  3. Map the table to an entity type so columnTypes can be omitted.
  4. Audit: for every i, Columns[i] must pair with ColumnTypes[i].

Example fix

// before (2 columns, 1 column type):
migrationBuilder.UpdateData(
    table: "Users",
    keyColumns: new[] { "Id" },
    keyValues: new object[,] { { 1 } },
    columns: new[] { "Name", "Email" },
    columnTypes: new[] { "nvarchar(100)" },
    values: new object[,] { { "Alice", "a@x.com" } });

// after (aligned):
migrationBuilder.UpdateData(
    table: "Users",
    keyColumns: new[] { "Id" },
    keyValues: new object[,] { { 1 } },
    columns: new[] { "Name", "Email" },
    columnTypes: new[] { "nvarchar(100)", "nvarchar(200)" },
    values: new object[,] { { "Alice", "a@x.com" } });
Defensive patterns

Strategy: validation

Validate before calling

static bool UpdateColumnTypesAreValid(string[] columns, string[]? columnTypes)
{
    return columnTypes == null || columnTypes.Length == columns.Length;
}

// usage
if (!UpdateColumnTypesAreValid(columns, columnTypes))
    throw new InvalidOperationException("columnTypes.Length must equal columns.Length (or be null)");

Try / catch

try
{
    await dbContext.Database.MigrateAsync();
}
catch (InvalidOperationException ex) when (ex.Message.Contains("number of column types") && ex.Message.Contains("data modification"))
{
    logger.LogError(ex, "An UpdateDataOperation has a columnTypes/columns length mismatch; fix the migration.");
    throw;
}

Prevention

When it happens

Trigger: A migration calls migrationBuilder.UpdateData with columnTypes: set to an array whose length differs from columns:. For example columns: new[] { "Name", "Email" } with columnTypes: new[] { "nvarchar(100)" }. Common after editing a migration and removing a column without removing its type.

Common situations: Manually adding columnTypes and miscounting. Editing a model-less migration and forgetting a column type entry. Changing the set of updated columns without updating the type array.

Related errors


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