entity framework在where子句中添加了额外的条件

我已经确定在执行以下表达式时:

int aNum = 52; var myArtifacts = mydbcontext.artifacts.Where(a => a.ParentID == aNum ).ToList(); 

在mysql上执行的查询是:

 SELECT `Extent1`.`ID`, `Extent1`.`ParentID` FROM `artifacts` AS `Extent1` WHERE ((`Extent1`.`ParentID` = 52) AND (52 IS NOT NULL)); 

任何人都可以解释为什么添加这个最后的额外条件?

AND(52不是空))

检查https://msdn.microsoft.com/en-us/library/system.data.entity.infrastructure.dbcontextconfiguration.usedatabasenullsemantics(v=vs.113).aspx

获取或设置一个值,该值指示在比较两个操作数时是否显示数据库空语义,这两个操作数都可能为空。 默认值为false。 例如(operand1 == operand2)将被翻译为:(operand1 = operand2)如果UseDatabaseNullSemantics分别为true(((operand1 = operand2)AND(NOT(operand1 IS NULL或operand2 IS NULL)))OR((operand1 IS) NULL)AND(operand2 IS NULL)))如果UseDatabaseNullSemantics为false。

如果当前行为困扰您,请考虑将UseDatabaseNullSemantics设置为true

 public class MyContext : DbContext { public MyContext() { this.Configuration.UseDatabaseNullSemantics = true; } } 

要么

 myDbContext.Configuration.UseDatabaseNullSemantics = true;