EF Core 2.2查询为可空列生成WHERE IS null

pan*_*nis 5 .net c# entity-framework-core .net-core

我有一张三列的桌子。客户ID为空。

public class ProductType
{
    public int Id { get; set; }

    public string Name { get; set; }

    public long? ClientId { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

当我运行以下代码时:

public async Task GetClientType(string productTypeName, long clientId)
        {
            var clientResult = await (from productType in context.ProductType
                                      where productType.Name == productTypeName
                                            && productType.ClientId == clientId
                                      select productType).FirstOrDefaultAsync();


            .......
        }
Run Code Online (Sandbox Code Playgroud)

尽管数据库包含此查询的结果,但我没有得到任何结果。然后,我检查了由EF Core生成的查询,并且看到生成了以下内容:

SELECT TOP(1) [productType].[Id], [productType].[Name], [productType].[ClientId]
FROM [ProductType] AS [productType]
WHERE [productType].[ClientId] IS NULL AND (([productType].[Name] = 'test client') AND ([productType].[ClientId] = 1))
Run Code Online (Sandbox Code Playgroud)

我可以看到问题出在Where子句上。为什么要产生这种说法?