0
我想要做的是能夠使用具有查詢轉移的投影。我已經成功做的第一級此使用投影僅使用AliasToBean變壓器,但是當我從一個細節類nhibenate Properties項目引發以下異常:詳細實體字段上的Nhibernate查詢映射投影
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>();
您應該投射到某種僅包含您需要的字段的DTO,而不是嘗試投影到實體本身。 –
@AndrewWhitaker問題是我的代碼是動態的,這只是一個示例,我想要做的是圖形模式,所以我可以用動態代碼生成它。有沒有辦法與現有的變壓器做到這一點,或者我應該建立自己的變壓器?! –