Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql server EF为可为空的实体生成不需要的SQL_Sql Server_Entity Framework_Entity Framework Core_Ef Core 2.2 - Fatal编程技术网

Sql server EF为可为空的实体生成不需要的SQL

Sql server EF为可为空的实体生成不需要的SQL,sql-server,entity-framework,entity-framework-core,ef-core-2.2,Sql Server,Entity Framework,Entity Framework Core,Ef Core 2.2,我有一个实体,它使用另一个可以为null的实体 class Parent { int? ChildId; Child Child; } class Child { decimal Property; } 我试图对子属性进行筛选,但它会生成一个奇怪的SQL Parents.Select(p => new { X = p.Child.Property <> 0 ? 1 : 0 }) Parents.Select(p=>new{X=p.Child.Prope

我有一个实体,它使用另一个可以为null的实体

class Parent 
{
   int? ChildId;
   Child Child;
}
class Child
{
   decimal Property;
}
我试图对子属性进行筛选,但它会生成一个奇怪的SQL

Parents.Select(p => new { X = p.Child.Property <> 0 ? 1 : 0  })
Parents.Select(p=>new{X=p.Child.Property 0?1:0})
但当我查看它生成的SQL时:

(([p.Child].[Property] <> 0.0) OR [p.Child].[Property] IS NULL).
(([p.Child].[Property]0.0)或[p.Child].[Property]为空)。
我想要的SQL是

(([p.Child].[Property] <> 0.0) OR [p.Child].[Property] IS NOT NULL)
(([p.Child].[Property]0.0)或[p.Child].[Property]不为空)

不使用
p.Child!=null&&p.Child.Property 0
。我尝试了这个方法,但它仍然添加了IS NULL条件。

这似乎是由于EF Core 2中的错误/缺陷造成的,该错误/缺陷已在EF Core 3中修复,因为它没有在那里发生

我尝试了所有可能的语法技巧,它继续插入
或isnull
条件,而不管
userrelationalnulls
选项如何

最后我得到了想要的结果

[p].[ChildId1] IS NOT NULL AND ([p.Child].[Property] <> 0.0
(正如你提到的,自然的写作方式

p.Child != null && p.Child.Property != 0

仍然包括
或为空
条件,尽管它对结果没有影响)

EF6或EF Core?如果是Core,什么版本?EF Core 2.2版感谢您对此进行调查。并确认在EF 3中不会发生这种情况。
p.Child != null && p.Child.Property != 0