我有一个简单的LINQ查询
from e in Employees
where e.DesignationID !=558
select e
在这里,DesignationID
是一个可空字段:
在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
中的相同查询被转换为:
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
处理空的方式与dbcontext
不同
发布于 2016-06-13 07:24:37
这种行为是可配置的,所以很可能是一个不同的默认(我不知道为什么默认是不同的)。
该控件由DbContextConfiguration.UseDatabaseNullSemantics属性提供:
获取或设置一个值,该值指示在比较两个操作数时是否显示数据库空语义,这两个操作数都可能为空。默认值为false。例如,(operand1 == operand2)将被翻译为:(operand1 = operand2),如果UseDatabaseNullSemantics为true ((operand1 = operand2)和(operand1为空或operand2为空))或((operand1为空)和(operand2为空))( UseDatabaseNullSemantics为false )。
要获得与第一个示例相同的翻译,应该将其设置为true
(例如,在db上下文构造函数中):
public YourDbContext()
{
// ...
this.Configuration.UseDatabaseNullSemantics = true;
}
https://stackoverflow.com/questions/37783677
复制相似问题