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 insertion operation on '{table}'. Provide the same number of column types and columns. What it means
For an InsertDataOperation where ColumnTypes is provided, EF checks that ColumnTypes.Length equals Columns.Length. Column types let the generator emit typed SQL when no model is available; an uneven list would leave some columns untyped or misaligned, so EF rejects it.
Solutions
- Provide exactly one column type per entry in columns, in the same order.
- Regenerate the migration so EF emits consistent columns/columnTypes.
- Drop columnTypes entirely if a model is attached (EF infers types), which also avoids this check.
Example fix
// before
migrationBuilder.InsertData(
table: "Products",
columns: new[] { "Id", "Name" },
columnTypes: new[] { "int" },
values: new object[,] { { 1, "A" } });
// after
migrationBuilder.InsertData(
table: "Products",
columns: new[] { "Id", "Name" },
columnTypes: new[] { "int", "nvarchar(100)" },
values: new object[,] { { 1, "A" } }); Defensive patterns
Strategy: validation
Validate before calling
// Validate column types align with columns for InsertData
static void ValidateInsertTypes(InsertDataOperation op)
{
if (op.ColumnTypes is { } types && types.Length != op.Columns.Length)
throw new ArgumentException(
$"InsertData on {op.Table}: {types.Length} types vs " +
$"{op.Columns.Length} columns.");
} Type guard
static bool InsertTypesOk(InsertDataOperation op)
=> op.ColumnTypes is null || op.ColumnTypes.Length == op.Columns.Length; Prevention
- Either always supply columnTypes (one per column) or omit them and rely on the model.
- Regenerate migrations after structural changes so types stay aligned.
- Lint migration files in CI for InsertData shape consistency.
When it happens
Trigger: migrationBuilder.InsertData(..., columns: new[] { "A","B" }, columnTypes: new[] { "int" }, ...) — the columnTypes array is shorter/longer than columns. Common in hand-written migrations or when a column was added without updating the types array.
Common situations: Editing a migration file by hand; programmatically generating InsertData where types and columns come from different sources; cross-provider migration where types were specified explicitly.
Related errors
- The data insertion operation on
- The number of key values
- The number of 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/193f9645298e6829.
Report an issue: GitHub.
Appendix: source
Thrown at src/EFCore.Relational/Migrations/MigrationsSqlGenerator.cs:884
/// <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(
RelationalStrings.InsertDataOperationNoModel(
FormatTable(operation.Table, operation.Schema ?? model?.GetDefaultSchema())));
}
var propertyMappings = operation.ColumnTypes == null
? GetPropertyMappings(operation.Columns, operation.Table, operation.Schema, model)
: null;
for (var i = 0; i < operation.Values.GetLength(0); i++)View on GitHub (pinned to 3a2006ef56)