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

时间:2015-12-03 17:40:32

标签: c# entity-framework ef-fluent-api

我有一个懒惰加载的一对一关系。当我调用select并尝试过滤optional不为null的位置时,它不会生成where子句,而不是null。它吸引了所有用户,无论他们是否有就业。

我的用户:

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);

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

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

<p>Mouse over the words to change the cursor.</p>
<span style="cursor:auto">auto</span><br>
<span style="cursor:crosshair">crosshair</span><br>
<span style="cursor:default">default</span><br>
<span style="cursor:e-resize">e-resize</span><br>
<span style="cursor:grab">grab</span><br>
<span style="cursor:help">help</span><br>
<span style="cursor:move">move</span><br>
<span style="cursor:n-resize">n-resize</span><br>
<span style="cursor:ne-resize">ne-resize</span><br>
<span style="cursor:nw-resize">nw-resize</span><br>
<span style="cursor:pointer">pointer</span><br>
<span style="cursor:progress">progress</span><br>
<span style="cursor:s-resize">s-resize</span><br>
<span style="cursor:se-resize">se-resize</span><br>
<span style="cursor:sw-resize">sw-resize</span><br>
<span style="cursor:text">text</span><br>
<span style="cursor:w-resize">w-resize</span><br>
<span style="cursor:wait">wait</span><br>
<span style="cursor:not-allowed">not-allowed</span><br>
<span style="cursor:no-drop">no-drop</span><br>

虽然Where子句只是被忽略仍然没有意义。