Given: EF Core 1.0.1 and the following classes:

public class User { public int UserId {get;set;} public string Login {get;set;} public string Password {get;set;} } public class DataContext:DbContext { public DbSet<User> Users { get; set; } } 

in the following code

 Datacontext dbcon = new DataContext(); var users = from p in dbcon.Users where p.Login=="login1" && p.Password=="pass1" select p; int number = users.Count(); 

an error occurs

InvalidOperationException: The "op_Equality" method.

why it happens? what should be done? complete exception description:

System.InvalidOperationException: "Op_Equality". at System.Linq.Expressions.Expression.GetMethodBasedBinaryOperator (ExpressionType binaryType, Expression left, Expression right, MethodInfo method, Boolean liftToNull) at System.Linq.Express_Enthal.Expression.Equal (Expression left, Expression right, Boolean liftToNull, method, Expression.Equal System.Linq.Expressions.Expression.MakeBinary (ExpressionType binaryType, Expression left, Expression of the right, Boolean liftToNull, MethodInfo method, LambdaExpression conversion) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.NavigationRewritExEx of ours, 5888, 58, 17, 58, 58. Graphics.Query.ExpressionVisitors. .Expressions.BinaryExpression.Accept (ExpressionVisitor visitor) at System.Linq.Expressions.ExpressionVisitor.Visit (Expression node) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.VisitBinary (Insthedurant.Internal.NavigationRewritingExpressionVisitor.VisitBinary (Inventory.Internal.NavigationRewritingExpressionVisitor.VisitBinary (Supplicat.Internal.NavigationRewritingExpressionVisitor.VisitBinary (Incremental ReplicationVis.Internal.NavigationRewritingExpression node)) .Accept (ExpressionVisitor visitor) at System.Linq.Expressions.Expres sionVisitor.com (Expression) 2 transformation) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.NavigationRewritingQueryModelVisitor.VisitWhereClause(WhereClause whereClause, QueryModel queryModel, Int32 index) at Remotion.Linq.QueryModelVisitorBase.VisitBodyClauses(ObservableCollection 1 bodyClauses, queryModel queryModel) at Remotion.Linq.QueryModelVisitorBase.VisitQueryModel ( queryModel queryModel) at Microsoft.EntityFrameworkCore.Query.ExpressionVisitors.Internal.NavigationRewritingExpressionVisitor.Rewrite (queryModel queryModel, queryModel parentQueryModel) at Microsoft. Entity's a Expression query inodetyprovider 1.<CompileQuery>b__0() at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQueryCore[TFunc](Object cacheKey, Func 1 compiler) at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute [TResult] (Expression query) at System.Linq.Queryable.Count [TSource] (IQueryable`1 source) at SolidCRMCore.Startup . <> c. <b__2_0> d.MoveNext () in C: \ Users \ documents \ visual studio 2017 \ Projects \ SolidCRMCore \ SolidCRMCore \ Startup.cs: line 78 --- exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task task) at Microsoft.AspNetCore.Builder.Extensions.MapMiddelow.AnDebuggerNotification (Task task) at Microsoft.AspNetCore.Builder.Extensions.MapMddelow.AnDebuggerNotification (Task Task) - End of stack trace from previous loc ation where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw () at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification (Task task) at Microsoft.AspNetCore.Diagn ..DeveloperExceptionPraframemad.Me.Mo.MoTe.MapToMeTeMeTeMeTeMeTeMeTeMeTeMeTeMeTeMeoMeToMetoramamak.

  • var users = db.Users.Where(x=>x.Login == "login1" && x.Password == "pass1").ToList(); IMHO, of course, but lambda syntax is easier to read in some cases - Bald
  • LINQ form does not matter. Error occurs when trying to check conditions on string fields - user242389
  • It is not compiled in the current view (no Users property) - Pavel Mayorov
  • Try to create a new database using EF. Will the error repeat or disappear? - Pavel Mayorov
  • Yes, and attach to the question, please, the full text of the exception (along with the stack trace!) - without it, fortune telling on the coffee grounds turns out - Pavel Mayorov

3 answers 3

like this:

  var users = con.Users.Where(p => p.Login.Equals(login)&&p.Password.Equals(password)); 

works, although the initial question, where to dig, remains open

    Apparently, this is an EF Core error at the query optimization stage. Try using a different version of the library.

    If for some reason it is not possible to upgrade, use Workaround with a call to the Equals method:

     var users = from p in dbcon.Users where p.Login.Equals(login) && p.Password.Equals(password) select p; 

    PS if the error remains on the latest version of the library - the place on the githab is in its list of bugs

    • so does not work either. but dbcon.Users.Where (p => p.Login.Equals (login) && p.Password.Equals (password)) works; - user242389
    • @ user242389 That's right? Constants don't work? Two times strange ... - Pavel Mayorov
    • @ user242389 and what about the new version? 1.0.1 is a rather old version ... - Pavel Mayorov
    • not in constant matter. the case in the application .were () - user242389
    • @ user242389 No, from this side there can be no problem in principle. The compiler generates exactly the same code for these two options. - Pavel Mayorov

    It may be worth doing this:

      DataContext dbcon = new DataContext(); // обрати внимание на эту строку, у тебя написано Datacontext вместо DataContext var users = from p in dbcon.Users where p.Login.Equals("login1") && p.Password.Equals("pass1") select p; int number = users.Count(); 

    And, as I understand it, the DataContext class should look like this:

     public class DataContext: DbContext { public DbSet<Call> Calls { get; set; } public DbSet<User> Users { get; set; } } 
    • The first line - made a mistake when retyping - is not the point; What is the meaning of Calls? - user242389
    • the form does not matter .... with Equals () the same error - user242389
    • Calls are used in your example. I added the Users property - it is compiled with it, and in this example it works. Is this the complete code? - Sv__t
    • Not that DbSet copied into the example. Yes, of course, there users - user242389