gpt4 book ai didi

c# - Entity Framework 在 where 子句上添加了一个额外的条件

转载 作者:可可西里 更新时间:2023-11-01 07:08:41 24 4
gpt4 key购买 nike

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

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 IS NOT NULL))

最佳答案

检查 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.

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

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

myDbContext.Configuration.UseDatabaseNullSemantics = true;

关于c# - Entity Framework 在 where 子句上添加了一个额外的条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45862865/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com