实体框架在where子句中添加了一个额外的条件

cno*_*nom 5 c# mysql entity-framework

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

int aNum = 52;
var myArtifacts = mydbcontext.artifacts.Where(a => a.ParentID == aNum ).ToList();
Run Code Online (Sandbox Code Playgroud)

在mysql上执行的查询是:

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

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

AND(52不是空))

Mih*_*scu 1

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

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

如果当前的行为困扰您,请考虑设置UseDatabaseNullSemanticstrue

public class MyContext : DbContext
{
    public MyContext()
    {
        this.Configuration.UseDatabaseNullSemantics = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

或者

myDbContext.Configuration.UseDatabaseNullSemantics = true;
Run Code Online (Sandbox Code Playgroud)