使用无状态会话字典值懒查找 [英] Lazy lookup of a dictionary value using a stateless session

查看:177
本文介绍了使用无状态会话字典值懒查找的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我成立了一个三元字典映射,使给定用户,我可以为所属的用户对象的每个实例检索设置。也就是说,我有这样的:

In my app, I set up a ternary dictionary mapping so that for a given user, I can retrieve "settings" for each instance of an object that belongs to the user. That is, I have something like:

public class User
{
    public virtual IDictionary<Baz, BazSettings> BazSettings { get; set; }

    //...



所以,每当我有一个巴兹对象,我可以通过 currentUser.BazSettings [巴兹]

我想能够使用无状态会话要做到这一点,但我得到一个 LazyInitializationException中使用此代码:

I would like to be able to use a stateless session to do this, but I get a LazyInitializationException with this code:

//int bazId;
using (IStatelessSession session = Global.SessionFactory.OpenStatelessSession())
{
    var currentUser = session.Get<User>(Membership.GetUser().ProviderUserKey);
    var baz = session.Get<Baz>(bazId);
    var bazSettings = currentUser.BazSettings[baz]; // raises `LazyInitializationException`

当我使用代替的Isession ,然后问题消失。

When I use instead an ISession, then the problem goes away.

完整NHibernate的错误消息包括文本没有会话或会话已关闭。这是有道理的,因为使用的是无状态的会话时,实体没有连接到会议。不过,我认为有使用无状态会话执行此查询的方式。

The full NHibernate error message includes the text "no session or session was closed". This makes sense because when using a stateless session, entities are not connected to the session. However, I would think that there is a way to use the stateless session to perform this lookup.

我如何使用无状态会话执行查找 currentUser.BazSettings [巴兹]

How do I use a stateless session to perform the lookup currentUser.BazSettings[baz]?

推荐答案

无状态会话不支持延迟加载,正是因为他们的无国籍:他们没有跟踪的任何关于与他们检索到的实体

Stateless sessions do not support lazy loading, exactly because they are stateless: they do not track anything about the entities retrieved with them.

的唯一途径使其工作是预先加载的集合。但是,为什么你的希望的使用无状态的会话,如果他们明确你需要什么不提供?

The only way to make it work is eager loading the collection. But why do you want to use stateless sessions, if they clearly do not provide what you need?

这篇关于使用无状态会话字典值懒查找的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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