如何使用 Fluent-nhibernate 将集合计数映射到实体
对于员工和下属 - 我想在一个查询中加载一名员工的下属数量。
public class Employee
{
public Name {get;set;}
public int NumberOfSubordinates {get;set;}
}
结果 SQL 应如下所示:
select e.name, (select count(*) from subordinate s where s.employee_id = e.id) NumberOfSubordinates
from employee e
group by e.name
order by NumberOfSubordinates desc
With employees and subordinates - I want to load an employee with the count of subordinates in one query.
public class Employee
{
public Name {get;set;}
public int NumberOfSubordinates {get;set;}
}
Resulting SQL should look like :
select e.name, (select count(*) from subordinate s where s.employee_id = e.id) NumberOfSubordinates
from employee e
group by e.name
order by NumberOfSubordinates desc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将此列映射为公式。
另一种方法是将下属映射为逆包并使用lazy="extra"。在这种情况下,
Subscribeds.Count
将执行 SQLcount(*)
,但不是作为初始加载的一部分。这种方法在 Fluent 中可能尚不可用。You could map this column as a Formula.
A different approach is to map Subordinates as an inverse bag and use lazy="extra". In this case
Subordinates.Count
will perform the SQLcount(*)
, though not as part of the initial load. This approach may not yet be available in Fluent.