Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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))

like image 962
cnom Avatar asked Aug 24 '17 13:08

cnom


1 Answers

Check 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. 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.

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

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

or

myDbContext.Configuration.UseDatabaseNullSemantics = true;
like image 68
Mihai Dinculescu Avatar answered Oct 18 '22 07:10

Mihai Dinculescu