当我执行以下查询时,我收到一个异常,告诉我feedItemQuery"包含多个项目(因此 SingleOrDefault 不起作用).
When I execute the following query, I get an exception telling me that 'feedItemQuery' contains multiple items (so SingleOrDefault doesn't work).
这是使用不带 DistinctRootEntity 转换器的 Criteria api 时的预期行为,但是在使用 linq 时,我希望获得一个根实体(FeedItem,其属性 Ads(来自 ICollection)包含所有广告).
This is expected behaviour when using the Criteria api WITHOUT the DistinctRootEntity transformer, but when using linq, I expect to get a single root entity (FeedItem, with the property Ads (of ICollection) containing all Ads).
有没有办法告诉 NHibernate.Linq 使用 DistinctRootEntity 转换器?
Is there a way to tell NHibernate.Linq to use the DistinctRootEntity transformer?
我的查询:
var feedItemQuery = from ad in session.Linq<FeedItem>().Expand("Ads") where ad.Id == Id select ad; var feedItem = feedItemQuery.SingleOrDefault(); // This fails !?映射:
<class name="FeedItem" table="FeedItems" proxy="IFeedItem"> <id name="Id" type="Guid"> <generator class="guidb"></generator> </id> ... <set name="Ads" table="Ads"> <key column="FeedItemId" /> <one-to-many class="Ad" /> </set> </class>提前致谢
推荐答案可以使用RegisterCustomAction方法设置结果转换器:
You can use the RegisterCustomAction method to set the result transformer:
var linqsession = session.Linq<FeedItem>(); linqsession.QueryOptions.RegisterCustomAction(c => c.SetResultTransformer(new DistinctRootEntityResultTransformer())); var feedItemQuery = from ad in linqsession.Expand("Ads") where ad.Id == Id select ad更多推荐
NHibernate Linq 和 DistinctRootEntity
发布评论