我有这个简单的 LINQ 查询
from e in Employees
where e.DesignationID !=558
select e
Here DesignationID
是一个可为空的字段:
In 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
处理 NULL 的方式与dbcontext
?
此行为是可配置的,因此很可能是不同默认值的问题(我不知道为什么默认值不同)。
该控制由提供DbContextConfiguration.UseDatabaseNullSemantics https://msdn.microsoft.com/query/dev14.query?appId=Dev14IDEF1&l=EN-US&k=k(System.Data.Entity.Infrastructure.DbContextConfiguration.UseDatabaseNullSemantics);财产:
获取或设置一个值,该值指示在比较两个操作数(这两个操作数都可能为空)时是否显示数据库空语义。默认值为 false。例如 (operand1 == operand2) 将被翻译为: (operand1 = operand2) 如果 UseDatabaseNullSemantics 为 true,则分别 (((operand1 = operand2) AND (NOT (operand1 IS NULL OR operand2 IS NULL))) OR ((operand1 IS NULL) AND (operand2 IS NULL))) 如果 UseDatabaseNullSemantics 为 false。
要获得与第一个示例相同的翻译,您应该将其设置为true
(例如在数据库上下文构造函数中):
public YourDbContext()
{
// ...
this.Configuration.UseDatabaseNullSemantics = true;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)