NHibernate映射问题-无法初始化代理-没有会话 [英] NHibernate mapping problem - Could not initialize proxy - no Session

查看:41
本文介绍了NHibernate映射问题-无法初始化代理-没有会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始学习NHibernate,并且正在学习以下教程.在我自己的学习项目中,我为自己解决了一个问题.我有两个桌子:

I have just started to learn NHibernate, and are following tutorials. On my own learning project, I have made up a problem for myself. I have two tables:

Team:
TeamId*
Name


Match:
MatchId*
TeamAId
TeamBId

模型实体为:

团队

public virtual int? TeamId { get; private set; }
public virtual string Name { get; set; }
public virtual IList<Match> HomeMatches { get; set; }
public virtual IList<Match> AwayMatches { get; set; }

匹配

public virtual int? MatchId { get; private set; }
public virtual Team TeamA { get; set; }
public virtual Team TeamB { get; set; }

这是我对Team的映射:

And this is my mapping for Team:

    <property name="Name" type="string">
        <column name="Name" sql-type="nvarchar" not-null="true"/>
    </property>


    <bag name="HomeMatches" inverse="true" cascade="all-delete-orphan">
        <key column="TeamAId" />
        <one-to-many class="Match"/>
    </bag>

    <bag name="AwayMatches" inverse="true" cascade="all-delete-orphan">
        <key column="TeamBId" />
        <one-to-many class="Match"/>
    </bag>

这是针对Match的:

And this is for Match:

    <many-to-one name="TeamA" class="Team">
        <column name="TeamAId" sql-type="int" not-null="true" />
    </many-to-one>

    <many-to-one name="TeamB" class="Team">
        <column name="TeamBId" sql-type="int" not-null="true" />
    </many-to-one>

我猜这不是正确的方法,因为我收到一条错误消息: NHibernate.LazyInitializationException:正在初始化[MyProject.Domain.Entities.Team#1]-无法初始化代理-没有会话.

I guess this isn't the right way to do it, since I get an error message: NHibernate.LazyInitializationException: Initializing[MyProject.Domain.Entities.Team#1]-Could not initialize proxy - no Session.

  1. 我的映射有问题吗?
  2. 如果没有,关于如何处理错误的任何建议?

谢谢.

推荐答案

问题不在于映射,而在于会话处理.

The problem is not with your mapping, but with your session handling.

您正在关闭会话,然后尝试访问延迟加载的媒体资源(团队).

You are closing the session and then trying to access a lazily-loaded property (a Team).

这篇关于NHibernate映射问题-无法初始化代理-没有会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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