延迟加载的一对一可选关系不在 select 上使用 where 子句

Lazy loaded One to One optional relationship not using where clause on select

我有一个延迟加载的一对一关系。当我对其调用 select 并尝试过滤可选项不为空的位置时,它不会生成非空的 where 子句。它吸引所有用户,无论他们是否有工作。

我的用户:

public class User
{
    public virtual Employment Employment { get; set; }
}

我的工作

public class Employment
{
    [InverseProperty("Employment")]
    public User User { get; set;  }
}

我的查询

await (from user in _dataContext.Users
       where user.Employment != null
       select user).ToListAsync()

这种方式也不行:

await _dataContext.Users
                  .Include(t => t.Employment)
                  .Where(t => t.Employment != null)
                  .ToListAsync();

在它生成的 sql 中,只是没有生成 where 子句。

在 Fluent 中我设置:

b.HasOne(u => u.Employment)
 .WithOne(t => t.User)
 .IsRequired(false);

我做错了什么?

我通过逆向得到了我需要的东西:

await _dataContext.Employment
                  .Include(t => t.User)
                  .Select(t => t.User)
                  .Include(t => t.Employment)
                  .ToListAsync();

尽管忽略 Where 子句仍然没有意义。