The following used to work fine in efcore 8.0 but in efcore 9.0, its throwing an exception:
System.InvalidOperationException: 'An exception was thrown while attempting to evaluate the LINQ query parameter expression '(SomeCondition() AndAlso (p.Dispute != null))'. See the inner exception for more information.'
Inner exception
InvalidOperationException: unbound variable: p
public class MyContext : DbContext
{
public DbSet<Trip> Trips { get; set; } = default!;
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) => optionsBuilder
.UseSqlServer(@"Server=.;Database=EFUnboundVarIssue;Trusted_Connection=True;MultipleActiveResultSets=true;encrypt=false")
.EnableSensitiveDataLogging()
.LogTo(p => Debug.WriteLine(p));
}
public class Trip
{
public int Id { get; set; }
public TripDispute? Dispute { get; set; }
}
public class TripDispute
{
public int Id { get; set; }
}
class Program
{
static void Main()
{
using (var db = new MyContext())
{
db.Database.EnsureDeleted();
db.Database.EnsureCreated();
db.Trips.Add(new Trip());
db.Trips.Add(new Trip { Dispute = new TripDispute() });
db.SaveChanges();
}
using (var db = new MyContext())
{
var q = db.Trips.Select(p => new
{
p.Id,
/* NOTE: If SomeCondition() evaluates to true, it works fine */
Dispute = SomeCondition() && p.Dispute != null ? p.Dispute!.Id : -1
});
var result = q.ToArray();
}
}
static bool SomeCondition() => Random.Shared.Next(1000) == 5;
}
I also tried calculating condition value before executing query:
var condition = SomeCondition();
var q = db.Trips.Select(p => new
{
p.Id,
Dispute = condition && p.Dispute != null ? p.Dispute!.Id : -1
});
var result = q.ToArray();
But still getting same error:
System.InvalidOperationException: 'An exception was thrown while attempting to evaluate the LINQ query parameter expression '(value(Program+<>c__DisplayClass0_0).condition AndAlso (p.Dispute != null))'. See the inner exception for more information.'
Inner exception
InvalidOperationException: unbound variable: p
I tried putting just false and still getting the same error:
using (var db = new MyContext())
{
var q = db.Trips.Select(p => new
{
p.Id,
Dispute = false && p.Dispute != null ? p.Dispute!.Id : -1
});
var result = q.ToArray();
}
System.InvalidOperationException: 'An exception was thrown while attempting to evaluate the LINQ query parameter expression '(False AndAlso (p.Dispute != null))'. See the inner exception for more information.'
Inner exception
InvalidOperationException: unbound variable: p
All of these were working in ef 8.0
stack trace
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.<Evaluate>g__EvaluateCore|70_0(Expression expression, String& parameterName, Boolean& isContextAccessor)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.Evaluate(Expression expression, String& parameterName, Boolean& isContextAccessor)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.Evaluate(Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.VisitConditional(ConditionalExpression conditional)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.Visit[T](ReadOnlyCollection`1 expressions, Func`2 elementVisitor, StateType& aggregateStateType, State[]& expressionStates, Boolean poolExpressionStates)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.Visit(ReadOnlyCollection`1 expressions, StateType& aggregateStateType, State[]& expressionStates, Boolean poolExpressionStates)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.VisitNew(NewExpression new)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.Visit(Expression expression, State& state)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.VisitLambda[T](Expression`1 lambda)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.Visit(Expression expression, State& state)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.VisitUnary(UnaryExpression unary)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.Visit(Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.Visit[T](ReadOnlyCollection`1 expressions, Func`2 elementVisitor, StateType& aggregateStateType, State[]& expressionStates, Boolean poolExpressionStates)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.Visit(ReadOnlyCollection`1 expressions, StateType& aggregateStateType, State[]& expressionStates, Boolean poolExpressionStates)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.VisitMethodCall(MethodCallExpression methodCall)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.Visit(Expression expression, State& state)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.ExtractParameters(Expression expression, IParameterValues parameterValues, Boolean parameterize, Boolean clearParameterizedValues, Boolean precompiledQuery, IReadOnlySet`1& nonNullableReferenceTypeParameters)
at Microsoft.EntityFrameworkCore.Query.Internal.ExpressionTreeFuncletizer.ExtractParameters(Expression expression, IParameterValues parameterValues, Boolean parameterize, Boolean clearParameterizedValues)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExtractParameters(Expression query, IParameterValues parameterValues, IDiagnosticsLogger`1 logger, Boolean compiledQuery, Boolean generateContextAccessors)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteCore[TResult](Expression query, Boolean async, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
at System.Collections.Generic.SegmentedArrayBuilder`1.AddNonICollectionRangeInlined(IEnumerable`1 source)
at System.Linq.Enumerable.<ToArray>g__EnumerableToArray|314_0[TSource](IEnumerable`1 source)
at Program.Main() in C:\Users\alaam\source\repos\EFUnboundVarIssue\Program.cs:line 25
Include verbose output
Please include --verbose output when filing bugs about the dotnet ef or Package Manager Console tools.
Use triple-tick fences for tool output. For example:
❯ dotnet ef dbcontext list --verbose
Using project 'C:\Users\alaam\source\repos\EFUnboundVarIssue\EFUnboundVarIssue.csproj'.
Using startup project 'C:\Users\alaam\source\repos\EFUnboundVarIssue\EFUnboundVarIssue.csproj'.
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\alaam\AppData\Local\Temp\tmpeznphc.tmp /verbosity:quiet /nologo C:\Users\alaam\source\repos\EFUnboundVarIssue\EFUnboundVarIssue.csproj
dotnet msbuild /target:GetEFProjectMetadata /property:EFProjectMetadataFile=C:\Users\alaam\AppData\Local\Temp\tmphzdqbp.tmp /verbosity:quiet /nologo C:\Users\alaam\source\repos\EFUnboundVarIssue\EFUnboundVarIssue.csproj
Build started...
dotnet build C:\Users\alaam\source\repos\EFUnboundVarIssue\EFUnboundVarIssue.csproj /verbosity:quiet /nologo /p:PublishAot=false
Build succeeded.
0 Warning(s)
0 Error(s)
Time Elapsed 00:00:00.96
Build succeeded.
dotnet exec --depsfile C:\Users\alaam\source\repos\EFUnboundVarIssue\bin\Debug\net9.0\EFUnboundVarIssue.deps.json --additionalprobingpath C:\Users\alaam\.nuget\packages --additionalprobingpath "C:\Program Files (x86)\Microsoft Visual Studio\Shared\NuGetPackages" --runtimeconfig C:\Users\alaam\source\repos\EFUnboundVarIssue\bin\Debug\net9.0\EFUnboundVarIssue.runtimeconfig.json C:\Users\alaam\.nuget\packages\dotnet-ef\9.0.0-rc.2.24474.1\tools\net8.0\any\tools\netcoreapp2.0\any\ef.dll dbcontext list --assembly C:\Users\alaam\source\repos\EFUnboundVarIssue\bin\Debug\net9.0\EFUnboundVarIssue.dll --project C:\Users\alaam\source\repos\EFUnboundVarIssue\EFUnboundVarIssue.csproj --startup-assembly C:\Users\alaam\source\repos\EFUnboundVarIssue\bin\Debug\net9.0\EFUnboundVarIssue.dll --startup-project C:\Users\alaam\source\repos\EFUnboundVarIssue\EFUnboundVarIssue.csproj --project-dir C:\Users\alaam\source\repos\EFUnboundVarIssue\ --root-namespace EFUnboundVarIssue --language C# --framework net9.0 --nullable --working-dir C:\Users\alaam\source\repos\EFUnboundVarIssue --verbose
Using assembly 'EFUnboundVarIssue'.
Using startup assembly 'EFUnboundVarIssue'.
Using application base 'C:\Users\alaam\source\repos\EFUnboundVarIssue\bin\Debug\net9.0'.
Using working directory 'C:\Users\alaam\source\repos\EFUnboundVarIssue'.
Using root namespace 'EFUnboundVarIssue'.
Using project directory 'C:\Users\alaam\source\repos\EFUnboundVarIssue\'.
Remaining arguments: .
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding DbContext classes in the project...
Found DbContext 'MyContext'.
Finding application service provider in assembly 'EFUnboundVarIssue'...
Finding Microsoft.Extensions.Hosting service provider...
No static method 'CreateHostBuilder(string[])' was found on class 'Program'.
No application service provider was found.
EFUnboundVarIssue.MyContext
Include provider and version information
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer 9.0.0-rc.2.24474.1
Target framework: .net 9.0
The following used to work fine in efcore 8.0 but in efcore 9.0, its throwing an exception:
I also tried calculating condition value before executing query:
But still getting same error:
I tried putting just
falseand still getting the same error:All of these were working in ef 8.0
stack trace
Include verbose output
Please include
--verboseoutput when filing bugs about thedotnet efor Package Manager Console tools.Use triple-tick fences for tool output. For example:
Include provider and version information
EF Core version:
Database provider: Microsoft.EntityFrameworkCore.SqlServer 9.0.0-rc.2.24474.1
Target framework: .net 9.0