简体   繁体   English

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

[英]Entity framework adds an extra condition on where clause

I have identified that when the following expression is executed: 我已经确定在执行以下表达式时:

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

on mysql the query executed is: 在mysql上执行的查询是:

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 IS NOT NULL)) AND(52不是空))

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

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. 默认值为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. 例如(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。

If the current behaviour is bothering you, consider setting UseDatabaseNullSemantics to true . 如果当前行为困扰您,请考虑将UseDatabaseNullSemantics设置为true

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

or 要么

myDbContext.Configuration.UseDatabaseNullSemantics = true;

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

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