NHibernate Linq 和 DistinctRootEntity [英] NHibernate Linq and DistinctRootEntity

查看:28
本文介绍了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="guid.comb"></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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆