我在构建查询时遇到问题。
这是架构:
表B 1 - < - > * TableA
public class TableB
{
public virtual IList<TableA> TableAData { get; set; }
}
mapping:
HasMany(x => x.TableAData).KeyColumn("TableAData_ID");
public class TableA
{
public virtual TableB ConnectedTableB_Item { get; set; }
}
mapping:
References(x => x.ConnectedTableB_Item, "ConnectedTableB_ID");
并且questy很简单 - 我需要加载那些TableB
计数为0的TableA
项
IList<TableA> tableA_Alias = null;
TableB tableB_Alias = null;
var query = Session.QueryOver<TableB>(() => tableB_Alias)
.JoinAlias(() => tableB_Alias.TableAData, () => tableA_Alias)
.Where(x => tableA_Alias.Count == 0);
但是我得到了例外
could not resolve property: Count of: TableA
我还尝试将IList<TableA> tableA_Alias
更改为TableA tableA_Alias
TableA tableA_Alias = null;
TableB tableB_Alias = null;
var query = Session.QueryOver<TableB>(() => tableB_Alias)
.JoinAlias(() => tableB_Alias.TableAData, () => tableA_Alias)
.Where(x => tableA_Alias == null);
然后我收到错误
Object reference not set to an instance of an object
(显然因为tableA_Alias
为空)。
我该如何解决?
答案 0 :(得分:1)
对于优化,您可能希望在父对象上创建readonly属性,然后映射到公式(子查询)。
public class Parent {
public virtual IList<Child> Children { get; set; }
public virtual int TotalChildren { get; set; }
}
并将其映射为:
Map(t => t.TotalChildren).Formula("SELECT COUNT(*) FROM ChildTable WHERE ChildTable.ParentId = ParentId");