NHibernate Linq 和 DistinctRootEntity

编程入门 行业动态 更新时间:2024-10-28 02:28:47
本文介绍了NHibernate Linq 和 DistinctRootEntity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

当我执行以下查询时,我收到一个异常,告诉我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

本文发布于:2023-05-26 16:20:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/265200.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:NHibernate   Linq   DistinctRootEntity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!