我想要做的是能够使用带有查询的投影。我仅使用 AliasToBean Transformer 在第一级使用投影成功地做到了这一点,但是当我对来自细节类 nhibenate 的属性进行投影时,会引发以下异常:
Could not find a setter for property 'FirstContact' in class 'Model.Personnel.Entities.Employee'
at NHibernate.Properties.ChainedPropertyAccessor.GetSetter(Type theClass, String propertyName)
at NHibernate.Transform.AliasToBeanResultTransformer.TransformTuple(Object[] tuple, String[] aliases)
at NHibernate.Loader.Criteria.CriteriaLoader.GetResultList(IList results, IResultTransformer customResultTransformer)
at NHibernate.Loader.Loader.ListIgnoreQueryCache(ISessionImplementor session, QueryParameters queryParameters)
at NHibernate.Loader.Loader.List(ISessionImplementor session, QueryParameters queryParameters, ISet`1 querySpaces, IType[] resultTypes)
at NHibernate.Loader.Criteria.CriteriaLoader.List(ISessionImplementor session)
at NHibernate.Impl.SessionImpl.List(CriteriaImpl criteria, IList results)
at NHibernate.Impl.CriteriaImpl.List(IList results)
at NHibernate.Impl.CriteriaImpl.List[T]()
at NHibernate.Criterion.QueryOver`1.List[U]()
at NHibernate.Criterion.QueryOver`1.NHibernate.IQueryOver<TRoot>.List[U]()
at Tests.Entities.EmployeeFacts.QueryEmployees()
这是我当前的代码:
Employee anEmployee = null;
Contact aContact = null;
session.QueryOver(() => anEmployee).Left
.JoinAlias(() => anEmployee.Contact, () => aContact)
.OrderBy(() => aGender.Name).Asc
.ThenBy(() => aContact.FirstContact).Asc
.SelectList(builder => builder.Select(() => aContact.FirstContact)
.WithAlias(() => aContact.FirstContact)
.Select(() => anEmployee.FirstName)
.WithAlias(() => anEmployee.FirstName))
.TransformUsing(Transformers.AliasToBean(typeof(Employee)))
.List<Employee>();