{"record":{"id":"b5896c6206b3dd88","repo":"dotnet/efcore","slug":"a-fromsqlexpression-has-an-invalid-arguments-expre","errorCode":null,"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.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs","lineNumber":493,"sourceCode":"                break;\n            }\n\n            case ConstantExpression { Value: object[] constantValues }:\n            {\n                substitutions = new string[constantValues.Length];\n                for (var i = 0; i < constantValues.Length; i++)\n                {\n                    var value = constantValues[i];\n                    var typeMapping = typeMappingSource.FindMapping(value.GetType());\n                    Check.DebugAssert(typeMapping is not null, \"Could not find type mapping for FromSql parameter\");\n                    substitutions[i] = ((CosmosTypeMapping)typeMapping).GenerateSqlLiteral(value);\n                }\n\n                break;\n            }\n\n            default:\n                throw new ArgumentOutOfRangeException(\n                    nameof(fromSqlExpression),\n                    fromSqlExpression.Arguments,\n                    CosmosStrings.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        _sqlBuilder.AppendLine(\"(\");\n\n        using (_sqlBuilder.Indent())\n        {\n            _sqlBuilder.AppendLines(sql);","sourceCodeStart":475,"sourceCodeEnd":511,"githubUrl":"https://github.com/dotnet/efcore/blob/3a2006ef569de08368d59db5e1468aa8f407e4f8/src/EFCore.Cosmos/Query/Internal/CosmosQuerySqlGenerator.cs#L475-L511","documentation":"FromSqlRaw/FromSqlInterpolated on Cosmos only accepts argument shapes that the provider can flatten into literal substitutions: a ConstantExpression holding a parameter array or raw values. If the Arguments node is a different Expression subtype the switch falls through to the default case and throws ArgumentOutOfRangeException, reporting the offending expression type and (if constant) its value type.","triggerScenarios":"Calling FromSqlRaw with a parameter object that is neither a plain object[] nor a DbParameter[], or composing a FromSql query in a way that wraps the argument array in another LINQ/Expression node before it reaches the provider.","commonSituations":"Migrating a relational FromSqlRaw call to Cosmos expecting identical argument flexibility, passing a single scalar where an array is needed, or building the sql parameter list dynamically and ending up with an unexpected expression shape.","solutions":["Pass arguments as a plain object[] to FromSqlRaw or use FromSqlInterpolated with an interpolated string.","Verify the parameter array element types each have a Cosmos type mapping (avoid unsupported CLR types).","Do not wrap the FromSql root in additional LINQ operators that rewrite the Arguments expression; project after the root instead."],"exampleFix":"// before\nvar rows = db.Users.FromSqlRaw(\"SELECT * FROM c WHERE c.id = {0}\", idScalar);\n// after\nvar rows = db.Users.FromSqlRaw(\"SELECT * FROM c WHERE c.id = {0}\", new object[] { idScalar });","handlingStrategy":"validation","validationCode":"static IQueryable<T> SafeFromSql<T>(DbSet<T> set, string sql, params object[] args)\n    where T : class\n{\n    if (args is null || Array.TrueForAll(args, a => a is null || a.GetType().IsPrimitive || a is string || a is DateTime || a is Guid))\n        return set.FromSqlRaw(sql, args ?? Array.Empty<object>());\n    throw new ArgumentException(\"FromSqlRaw arguments must be simple constants.\", nameof(args));\n}","typeGuard":"static bool IsSupportedFromSqlArgs(Expression e)\n    => e is ConstantExpression c && (c.Value is object[] || c.Value is DBNull || c.Value is null);","tryCatchPattern":null,"preventionTips":["Prefer FromSqlInterpolated to get compile-time parameterization.","Never pass a single scalar where an object[] is expected.","Keep FromSql at the root of the query; project afterward."],"tags":["cosmos","fromsql","query-translation","parameters"],"backgroundTag":null,"analyzedSha":"3a2006ef569de08368d59db5e1468aa8f407e4f8","analyzedAt":"2026-08-11T23:42:04.146Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}