实体框架在where子句中添加了额外条件 [英] Entity framework adds an extra condition on where clause

查看:163
本文介绍了实体框架在where子句中添加了额外条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have identified that when the following expression is executed:

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

在mysql上执行的查询是:

on mysql the query executed is:

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

任何人都可以解释一下为什么添加了最后一个额外条件吗?

Can anyone explain please why this last extra condition is added?

AND(52不为空)

AND (52 IS NOT NULL))

推荐答案

检查

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

Gets or sets a value indicating whether database null semantics are exhibited when comparing two operands, both of which are potentially nullable. The default value is false. For example (operand1 == operand2) will be translated as: (operand1 = operand2) if UseDatabaseNullSemantics is true, respectively (((operand1 = operand2) AND (NOT (operand1 IS NULL OR operand2 IS NULL))) OR ((operand1 IS NULL) AND (operand2 IS NULL))) if UseDatabaseNullSemantics is false.

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

If the current behaviour is bothering you, consider setting UseDatabaseNullSemantics to true.

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

myDbContext.Configuration.UseDatabaseNullSemantics = true;

这篇关于实体框架在where子句中添加了额外条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆