Simple.Data 组织关系管理。无法绑定多部分标识符

Simple.Data ORM. The multi-part identifier could not be bound

我正在使用 Simple.Data ORM。我正在尝试从两个连接表进行查询。此查询工作正常:

dynamic alias;
var candidatesRec = db.dbo.Candidates
            .FindAll(db.dbo.Candidates.CommonOfferId == commonOfferId 
                        && db.dbo.CandidateProfiles.CandidateId == null)
            .LeftJoin(db.dbo.CandidateProfiles, out alias)
            .On(db.dbo.Candidates.Id == alias.CandidateId)
            .Select(
                db.dbo.Candidates.Id,
                db.dbo.Candidates.Email
            )
            .OrderByDescending(db.dbo.Candidates.ApplicationDate)

但是当添加这一行时:

.Skip((pageNumber - 1) * pageSize)

我遇到了这个异常:

The multi-part identifier \"dbo.CandidateProfiles.CandidateId\" could not be bound.

我试图明确地将 0、1 和其他一些数字传递给 Skip,但我总是遇到同样的异常。

我的测试查询应该 return 4 个元素,而我跳过了 0 个元素(在正常使用中它可以更多)。

附加信息:CandidateProfiles 有来自 Candidates 的外键,它的 CandidateId 可以为空。

编辑:我们已经解决了这个问题,但我真的很好奇为什么这个不起作用。 Simple.Data一开始觉得很好玩,现在不知道以后会不会用

这是对 http://www.sql-server-helper.com/error-messages/msg-4104.aspx 中的 SQL 错误的解释:

Another way of getting the error is when an alias has been assigned to a table referenced in the FROM clause of a statement and the table is used as a prefix of a column instead of using the alias.

虽然这不是您的情况,但非常接近。我在您的代码示例中看到的问题是在 FindAll 调用中:

        .FindAll(db.dbo.Candidates.CommonOfferId == commonOfferId 
                    && db.dbo.CandidateProfiles.CandidateId == null)
        .LeftJoin(db.dbo.CandidateProfiles, out alias)

因为 FindAll 使用完全限定名称 "db.dbo.CandidateProfiles" 并且每个其他引用都使用在随后的 LeftJoin[=33] 中定义的别名=],生成的 where 子句中的出现不使用别名,您最终得到别名和显式 table 引用的混合和匹配,即 SQL 不喜欢。

我认为它只在添加 "Skip" 之后才出现在你的案例中的原因是最终生成了非常不同的 SQL (我的猜测是只有在添加 Skip 之后是实际使用的别名)。

不明白CandidateProfiles.CandidateId == null条件的用途(因为结合On[=33中的条件=] 调用我看不出这会如何 return 任何结果),但我建议在 On 调用中对加入的 table 添加限制它最终出现在 on 子句中而不是 where 子句中(然后使用别名而不是完全限定名称)

.LeftJoin(db.dbo.CandidateProfiles, out alias)  
.On(db.dbo.Candidates.Id == alias.CandidateId && alias.CandidateId == null)