{"record":{"id":"b497ecfa73c5690f","repo":"dotnet/efcore","slug":"invalidfromsqlarguments","errorCode":"InvalidFromSqlArguments","errorMessage":"A FromSqlExpression has an invalid arguments expression type '{expressionType}' or value type '{valueType}'.","messagePattern":"A FromSqlExpression has an invalid arguments expression type '(.+?)' or value type '(.+?)'\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/EFCore.Relational/Query/QuerySqlGenerator.cs","lineNumber":510,"sourceCode":"                for (var i = 0; i < constantValues.Length; i++)\n                {\n                    switch (constantValues[i])\n                    {\n                        case RawRelationalParameter rawRelationalParameter:\n                            substitutions[i] = _sqlGenerationHelper.GenerateParameterNamePlaceholder(rawRelationalParameter.InvariantName);\n                            _relationalCommandBuilder.AddParameter(rawRelationalParameter);\n                            break;\n                        case SqlConstantExpression sqlConstantExpression:\n                            substitutions[i] = sqlConstantExpression.TypeMapping!.GenerateSqlLiteral(sqlConstantExpression.Value);\n                            break;\n                    }\n                }\n\n                break;\n            }\n\n            default:\n                throw new ArgumentOutOfRangeException(\n                    nameof(fromSqlExpression),\n                    fromSqlExpression.Arguments,\n                    RelationalStrings.InvalidFromSqlArguments(\n                        fromSqlExpression.Arguments.GetType(),\n                        fromSqlExpression.Arguments is ConstantExpression constantExpression\n                            ? constantExpression.Value?.GetType()\n                            : null));\n        }\n\n        // ReSharper disable once CoVariantArrayConversion\n        // InvariantCulture not needed since substitutions are all strings\n        sql = string.Format(sql, substitutions);\n\n        _relationalCommandBuilder.AppendLines(sql);\n    }\n\n    /// <summary>\n    ///     Generates SQL for a user-provided SQL query.","sourceCodeStart":492,"sourceCodeEnd":528,"githubUrl":"https://github.com/dotnet/efcore/blob/dbf9771522148d61a2467854921bd5dc6f6e6916/src/EFCore.Relational/Query/QuerySqlGenerator.cs#L492-L528","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Update EF Core (and your provider) to the latest patch - this is typically an internal bug fixed in a release.","Simplify the FromSql call: use FromSqlInterpolated (which produces a known arguments shape) instead of manual parameter construction.","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).","Avoid constructing FromSqlExpression directly; rely on the public FromSqlRaw/FromSqlInterpolated APIs."],"exampleFix":"// before - parameter shape EF can't represent (internal bug surface)\nvar q = ctx.Blogs.FromSqlRaw(\"SELECT * FROM Blogs WHERE Id = {0}\", someUnsupportedParam);\n\n// after - use interpolated form which EF builds with a known arguments shape\nvar id = 42;\nvar q = ctx.Blogs.FromSqlInterpolated($\"SELECT * FROM Blogs WHERE Id = {id}\");","handlingStrategy":"validation","validationCode":"// Prefer FromSqlInterpolated, which EF builds with a known arguments shape.\nvar q = ctx.Blogs.FromSqlInterpolated($\"SELECT * FROM Blogs WHERE Id = {id}\");\n// Avoid constructing FromSqlExpression directly or passing exotic parameter shapes.","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use the public FromSqlRaw/FromSqlInterpolated APIs; never construct FromSqlExpression directly.","Prefer FromSqlInterpolated for parameterized raw SQL.","Keep EF Core and your provider on compatible versions.","Report internal argument-shape bugs to EF via the feedback link with a minimal repro."],"tags":["ef-core","fromsql","sql-generation","provider","parameters"],"analyzedSha":"dbf9771522148d61a2467854921bd5dc6f6e6916","analyzedAt":"2026-08-06T20:46:03.226Z","schemaVersion":2},"datasetVersion":"2026-08-07T02:17:10.218Z"}