locked
System.InvalidOperationException: Variable '' of type '' referenced from scope '', but it is not defined RRS feed

  • Question

  • I am trying to use entity framework global filter using expression in c#. I can able to frame expected expression either single or combined expression without any problem.

    I applied the query filter for all the entity using a property in model builder as below

    1. Created a dummy class with property for whom query filter will be applied. (i.e. IsActive)
    2. Expression with that dummy class with be overridden using "VisitMember()" to create new expression which actually convert the member of dummy class to the entity type.

    Note: I am tried to apply simple query fiter like "i => i.IsActive == true". Facing this error for both single and combined expression using "AndAlso".

    As checked the expected query filter has been applied to the respective tables without any errors.

     protected override Expression VisitMember(MemberExpression expression)
        {
            if (expression.Member.DeclaringType == this.baseEntityType)
            {
                var convertedParam = Expression.Parameter(entityType, this.param);
                return Expression.PropertyOrField(convertedParam, expression.Member.Name);
            }
    
            return base.VisitMember(expression);
        }

    I have expected the result from the table by applying the query filter but when I tried to access data from the table using linq query I am getting the following error. Can you anyone suggest your solution to resolve this error?

       at System.Linq.Expressions.Compiler.VariableBinder.Reference(ParameterExpression node, VariableStorageKind storage)
       at System.Linq.Expressions.Compiler.VariableBinder.VisitParameter(ParameterExpression node)
       at System.Linq.Expressions.ParameterExpression.Accept(ExpressionVisitor visitor)
       at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
       at System.Linq.Expressions.ExpressionVisitor.VisitMember(MemberExpression node)
       at System.Linq.Expressions.MemberExpression.Accept(ExpressionVisitor visitor)
       at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
       at System.Linq.Expressions.ExpressionVisitor.VisitBinary(BinaryExpression node)
       at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
       at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
       at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
       at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
       at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
       at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
       at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
       at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
       at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
       at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
       at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
       at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
       at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
       at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
       at System.Dynamic.Utils.ExpressionVisitorUtils.VisitArguments(ExpressionVisitor visitor, IArgumentProvider nodes)
       at System.Linq.Expressions.ExpressionVisitor.VisitMethodCall(MethodCallExpression node)
       at System.Linq.Expressions.MethodCallExpression.Accept(ExpressionVisitor visitor)
       at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
       at System.Linq.Expressions.ExpressionVisitor.Visit(ReadOnlyCollection`1 nodes)
       at System.Linq.Expressions.Compiler.VariableBinder.VisitLambda[T](Expression`1 node)
       at System.Linq.Expressions.Expression`1.Accept(ExpressionVisitor visitor)
       at System.Linq.Expressions.Compiler.VariableBinder.Visit(Expression node)
       at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda)
       at System.Linq.Expressions.Expression`1.Compile(Boolean preferInterpretation)
       at System.Linq.Expressions.Expression`1.Compile()
       at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateExecutorLambda[TResults]()
       at Microsoft.EntityFrameworkCore.Query.RelationalQueryModelVisitor.CreateExecutorLambda[TResults]()
       at Microsoft.EntityFrameworkCore.Query.EntityQueryModelVisitor.CreateQueryExecutor[TResult](QueryModel queryModel)
       at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](QueryModel queryModel)
       --- End of stack trace from previous location where exception was thrown ---
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](Expression query, IQueryModelGenerator queryModelGenerator, IDatabase database, IDiagnosticsLogger`1 logger, Type contextType)
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass13_0`1.<Execute>b__0()
       at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func`1 compiler)
       at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
       at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
       at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryProvider.Execute[TResult](Expression expression)
       at Remotion.Linq.QueryableBase`1.GetEnumerator()
       at Microsoft.EntityFrameworkCore.Internal.InternalDbSet`1.System.Collections.Generic.IEnumerable<TEntity>.GetEnumerator()
       at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
       at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)

    Thanks in advance.

    Sunday, September 8, 2019 5:58 PM

All replies

  • Hi Ragunaathan M P,

    Thank you for posting here.

    Since this thread is related to Entity framework, I will move it to ADO.NET Entity Framework and LINQ to Entities forum.

    The Visual C# forum discusses and asks questions about the C# programming language, IDE, libraries, samples, and tools.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, September 9, 2019 8:53 AM
  • Hi,

    In addition to the error info, could you provide the exception you got?

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, September 10, 2019 1:37 AM