dotnet/efcore · error · ArgumentOutOfRangeException

InvalidFromSqlArguments

InvalidFromSqlArguments

Error message

A FromSqlExpression has an invalid arguments expression type '{expressionType}' or value type '{valueType}'.

What it means

Thrown by QuerySqlGenerator.GenerateFromSql (QuerySqlGenerator.cs:510) when a FromSqlExpression's Arguments expression is neither a ConstantExpression holding a CompositeRelationalParameter nor a ConstantExpression holding an object[] of RawRelationalParameter/SqlConstantExpression. This is an internal contract violation: FromSql arguments are produced by EF's own translation, so an unrecognized shape indicates a bug in EF or a provider, not user SQL.

Source

Thrown at src/EFCore.Relational/Query/QuerySqlGenerator.cs:510

                for (var i = 0; i < constantValues.Length; i++)
                {
                    switch (constantValues[i])
                    {
                        case RawRelationalParameter rawRelationalParameter:
                            substitutions[i] = _sqlGenerationHelper.GenerateParameterNamePlaceholder(rawRelationalParameter.InvariantName);
                            _relationalCommandBuilder.AddParameter(rawRelationalParameter);
                            break;
                        case SqlConstantExpression sqlConstantExpression:
                            substitutions[i] = sqlConstantExpression.TypeMapping!.GenerateSqlLiteral(sqlConstantExpression.Value);
                            break;
                    }
                }

                break;
            }

            default:
                throw new ArgumentOutOfRangeException(
                    nameof(fromSqlExpression),
                    fromSqlExpression.Arguments,
                    RelationalStrings.InvalidFromSqlArguments(
                        fromSqlExpression.Arguments.GetType(),
                        fromSqlExpression.Arguments is ConstantExpression constantExpression
                            ? constantExpression.Value?.GetType()
                            : null));
        }

        // ReSharper disable once CoVariantArrayConversion
        // InvariantCulture not needed since substitutions are all strings
        sql = string.Format(sql, substitutions);

        _relationalCommandBuilder.AppendLines(sql);
    }

    /// <summary>
    ///     Generates SQL for a user-provided SQL query.

View on GitHub (pinned to dbf9771522)

Solutions

  1. Update EF Core (and your provider) to the latest patch - this is typically an internal bug fixed in a release.
  2. Simplify the FromSql call: use FromSqlInterpolated (which produces a known arguments shape) instead of manual parameter construction.
  3. Reduce the number/types of parameters to isolate the case that triggers the bad arguments expression; file an EF issue with the repro (link aka.ms/efcorefeedback).
  4. Avoid constructing FromSqlExpression directly; rely on the public FromSqlRaw/FromSqlInterpolated APIs.

Example fix

// before - parameter shape EF can't represent (internal bug surface)
var q = ctx.Blogs.FromSqlRaw("SELECT * FROM Blogs WHERE Id = {0}", someUnsupportedParam);

// after - use interpolated form which EF builds with a known arguments shape
var id = 42;
var q = ctx.Blogs.FromSqlInterpolated($"SELECT * FROM Blogs WHERE Id = {id}");
Defensive patterns

Strategy: validation

Validate before calling

// Prefer FromSqlInterpolated, which EF builds with a known arguments shape.
var q = ctx.Blogs.FromSqlInterpolated($"SELECT * FROM Blogs WHERE Id = {id}");
// Avoid constructing FromSqlExpression directly or passing exotic parameter shapes.

Prevention

When it happens

Trigger: FromSqlRaw/FromSqlInterpolated query whose parameter arguments expression was constructed with an unsupported type/value during translation. Essentially only reachable via a bug in EF's FromSql parameter handling or a provider/extension that builds FromSqlExpression with malformed Arguments.

Common situations: EF Core or provider bug where Arguments is mis-typed; custom code constructing FromSqlExpression with arbitrary arguments; version mismatch between EF runtime and a provider that builds FromSql arguments differently; edge cases in parameterized FromSql after an upgrade.

Related errors


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