dbcontext和objectcontext中的NULL处理 [英] NULL handling in dbcontext and objectcontext
问题描述
我有这个简单的LINQ查询
I have this simple LINQ query
from e in Employees
where e.DesignationID !=558
select e
这里指定ID
是一个可空字段:
在 objectcontext
中将查询转换为:
SELECT
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[EmployeeCode] AS [EmployeeCode],
[Extent1].[EmployeeName] AS [EmployeeName],
[Extent1].[DesignationID] AS [DesignationID]
FROM [dbo].[setupEmployees] AS [Extent1]
WHERE 558 <> [Extent1].[DesignationID]
虽然在 dbcontext
它被翻译成:
While the same query in dbcontext
it is translated to:
SELECT
[Extent1].[EmployeeID] AS [EmployeeID],
[Extent1].[EmployeeCode] AS [EmployeeCode],
[Extent1].[EmployeeName] AS [EmployeeName],
[Extent1].[DesignationID] AS [DesignationID]
FROM [dbo].[setupEmployees] AS [Extent1]
WHERE NOT ((558 = [Extent1].[DesignationID]) AND ([Extent1].[DesignationID] IS NOT NULL))
为什么 objectcontext
处理NULL不同于 dbcontext
?
Why does objectcontext
handle NULL differently than dbcontext
?
推荐答案
这个行为是可配置的,所以很有可能是一个不同的默认(我不知道为什么默认是不同的)。
This behavior is configurable, so most likely it's a matter of a different default (I don't know why the default is different).
该控件由 DbContextConfiguration.UseDatabaseNullSemantics 属性: p>
The control is provided by the DbContextConfiguration.UseDatabaseNullSemantics property:
获取或设置一个值,指示在比较两个可能为空的操作数时是否显示数据库空语义。 默认值为false 。 ((operand1 = operand2)AND(NOT(operand1 IS NULL OR operand2 IS NULL)))OR((operand1 IS is(operand1 = operand2))将被转换为(operand1 = operand2),如果UseDatabaseNullSemantics为true 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.
要获得与第一个示例相同的翻译,您应该将其设置为 true
(例如在您的数据库上下文构造函数中):
To get the same translation as in your first example, you should set it to true
(for instance inside your db context constructor):
public YourDbContext()
{
// ...
this.Configuration.UseDatabaseNullSemantics = true;
}
这篇关于dbcontext和objectcontext中的NULL处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!