简体   繁体   English

实体框架+ Lambda表达式null比较不起作用

[英]Entity framework + Lambda expression null comparison not working

i have a simple select: 我有一个简单的选择:

this works: 这工作:

User = ent.e_user.FirstOrDefault(x => x.usr_name == UserName && x.password == null);

Even if var Password is equal to null 即使var Password等于null

this doesn't work: 这不起作用:

String Password = null;

if(Password == null)
{
 //it enters here
}

    User = ent.e_user.FirstOrDefault(x => x.usr_name == UserName && x.password == Password);

Using the variable, it's not returning. 使用变量,它不会返回。

any idea? 任何想法?

Are you running against a SQL Server database? 您是否针对SQL Server数据库运行? Do you have access to the SQL Server Profiler? 您是否可以访问SQL Server Profiler?

I think a look at the SQL being executed would tell you a lot. 我想看看正在执行的SQL会告诉你很多。 For example, if Password is passed as a parameter (as I believe it would be) is it compared to the column password with a simple "="? 例如,如果Password作为参数传递(我相信它会是),它与列password比较简单的“=”? That will always return false, because 这将总是返回false,因为

SET @1 = Null;
SELECT *
FROM   table
WHERE  password = @1;

... will never return any records, even if every record has a null password field. ...永远不会返回任何记录,即使每个记录都有一个空password字段。

You could try something like this: 你可以尝试这样的事情:

User = ent.e_user.FirstOrDefault(
    x => x.usr_name == UserName && 
        (x.password == Password || (x.password == null && Password == null)
);

A detailed explanation can be found in this article: NULL Value Handling in Entity Framework . 可以在本文中找到详细说明: 实体框架中的NULL值处理 Beware that EF 5.0, 6.0 and 6.1 handle the nullable values differently. 请注意,EF 5.0,6.0和6.1可以不同的方式处理可空值。 In EF 5.0, you will need to manually test for nulls like the answer above; 在EF 5.0中,您需要手动测试空值,如上面的答案; an equation comparison between two variables does not test for nulls by default. 默认情况下,两个变量之间的等式比较不测试空值。 You can also turn on the UseCSharpNullComparisonBehavior property manually in the DbContext.ContextOptions to achieve the same effect. 您也可以手动打开UseCSharpNullComparisonBehavior财产在DbContext.ContextOptions来达到同样的效果。 In EF 6.0, null comparison is turned on by default, but probably over-aggressively and even on non-nullable columns, resulting in slower performance. 在EF 6.0中,默认情况下启用空比较,但可能过于积极甚至在非可空列上,导致性能降低。 EF 6.1 is supposed to have tweaked the algorithm to only test nulls when truly needed. EF 6.1应该将算法调整为仅在真正需要时测试空值。

The && operator is an 'early terminating' operator. &&运算符是“早期终止”运算符。 That means that if the result of the first condition is false (ie, usr_name is not the same value as UserName ), then it will never check the password - there's no point. 这意味着如果第一个条件的结果为false (即, usr_nameUserName值不同),那么它将永远不会检查密码 - 没有意义。 If the first part of the 'and' is false then the result of evaluating all operators is false. 如果'和'的第一部分为假,则评估所有运算符的结果为false。 If you are determined to check the password, either swap the order of the usr_name and password check, change the && to & or name sure that x.usr_name evaluates to 'true' 如果您决定检查密码,请交换usr_name和密码检查的顺序,将&&更改为&或命名,确保x.usr_name计算结果为'true'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM