我在使用 QueryOver 的 JoinAlias 和 List 属性的 SelectList 在 Nhibernate 中构建投影时遇到问题。
我有具有 IList 属性 PageHeaders 的域对象 PageRic:
public class PageRic: AuditableLegacy
{
public virtual string Ric { get; set; }
public virtual McdDistId McdDistId {get; set; }
public virtual IList<PageHeader> PageHeaders { get; set; }
public virtual string Pointer { get; set; }
public virtual bool Dup { get; set; }
public virtual int Throttle { get; set; }
}
PageHeaders 属性映射为 HasMany:
public class PageRicMapping : ClassMap<PageRic>
{
public PageRicMapping()
{
Table("PAGE_RICS");
Id(x => x.Ric, "RIC").Not.Nullable();
References(x => x.McdDistId, "MCD_DIST_ID")
.Not.LazyLoad()
.Nullable();
HasMany(x => x.PageHeaders)
.KeyColumn("RIC")
.Fetch.Subselect()
.Inverse()
.Cascade.AllDeleteOrphan()
.Not.LazyLoad();
Map(x => x.Pointer, "POINTER");
Map(x => x.Dup, "DUP").Not.Nullable().CustomType<YesNoType>();
Map(x => x.Throttle, "THROTTLE");
}
}
当我执行此查询时:
PageRicDTO pageRicDTO = null;
McdDistId McdDistAlias = null;
var results = Session.QueryOver<PageRic>()
.JoinAlias(x => x.McdDistId, () => McdDistAlias)
.SelectList(list => list
.Select(x => McdDistAlias.Id).WithAlias(() => pageRicDTO.McdDistId)
.Select(x => McdDistAlias.ShortName).WithAlias(() => pageRicDTO.McdDistName)
.Select(x => x.Ric).WithAlias(() => pageRicDTO.Ric)
.Select(x => x.Throttle).WithAlias(() => pageRicDTO.Throttle)
.Select(x => x.Rowstamps).WithAlias(() => pageRicDTO.Rowstamps)
.Select(x => x.PageHeaders).WithAlias(() => pageRicDTO.PageHeaders)
)
.TransformUsing(Transformers.AliasToBean<PageRicDTO>())
.List<PageRicDTO>();
我得到 IndexOutOfRangeException: Index was outside the bounds of the array。这是我在日志文件中得到的内容:
2012-04-18 15:00:38,823 [CurrentAppDomainHost.ExecuteNodes] 调试 NHibernate.SQL - 选择 mcddistali1_.MCD_DIST_ID 作为 y0_,mcddistali1_.SHORT_NAME 作为 y1_,this_.RIC 作为 y2_,this_.THROTTLE 作为 y3_,this_.ROWSTAMPS 作为 y4_ , this_.RIC as y5_ FROM PAGE_RICS this_ inner join MCD_DIST_IDS mcddistali1_ on this_.MCD_DIST_ID=mcddistali1_.MCD_DIST_ID
2012-04-18 15:00:39,306 [CurrentAppDomainHost.ExecuteNodes] WARN NHibernate.Util.ADOExceptionReporter - System.IndexOutOfRangeException:索引超出了数组的范围。在 NHibernate.Loader.Criteria.CriteriaLoader.GetResultColumnOrRow(Object[] row, IResultTransformer resultTransformer, IDataReader rs, ISessionImplementor session) 在 NHibernate.Loader.Loader.GetRowFromResultSet(IDataReader resultSet, ISessionImplementor session, QueryParameters queryParameters, LockMode[] lockModeArray, EntityKey optionalObjectKey , IList hydradObjects, EntityKey[] 键, Boolean returnProxies) 在 NHibernate.Loader.Loader.DoQuery(ISessionImplementor session, QueryParameters queryParameters, Boolean returnProxies) 在 NHibernate.Loader.Loader.DoQueryAndInitializeNonLazyCollections(ISessionImplementor session, QueryParameters queryParameters,
当我注释掉 .Select(x => x.PageHeaders).WithAlias(() => pageRicDTO.PageHeaders) 时,我得到了 DTO 对象。
你能帮我找到这个问题的解决方案吗?