Nhibernate - 未能懒惰地初始化集合...没有会话或会话被关闭
我收到此错误,想知道是否有人知道如何调试此错误。
谢谢
初始化[GL.Objects.Profile`1[[GL.Objects.Education.Education, GL.对象,版本=0.1.1.0, 文化=中立, PublicKeyToken=ebf25c7be4be0c91]]#289289]
-未能延迟初始化角色集合:
GL.Objects.Profile`1[[GL.Objects.Education.Education, GL.对象,版本=0.1.1.0, 文化=中立, PublicKeyToken=ebf25c7be4be0c91]].Profileables,
没有会话或会话已关闭”
启动会话
var watsonService = new WatsonService();
此代码保存实例化的对象。
watsonService.SaveEducation(e);
epf1.Profilables.Add(e);
watsonService.SaveEducationProfile(epf1);
epf2 = watsonService.GetEducationProfile(epf1.ID.Value);
第一个断言通过得很好。 epf2 有一个 id 并被实例化。
Assert.AreEqual(epf1.ID, epf2.ID);
此断言失败。 Profileables 与 id 已成功插入的复合表相匹配。但现在,当我尝试访问集合中的第一个元素时,它失败了。
Assert.AreEqual(epf1.Profilables[0].ID, epf2.Profilables[0].ID);
这是 GetEducationProfile 方法调用的方法实现。
public T Get<T>(int id) where T : IDataObject
{
return (T)_session.Load(typeof(T), id, LockMode.Read);
}
这是 Fluent 映射文件中构造函数的定义。
public EducationProfileMap()
{
Table("Profile");
Id(x => x.ID)
.Column("ProfileID")
.GeneratedBy
.HiLo(FluentConst.HILO_TABLE,
FluentConst.NEXTHI_COLUMN,
FluentConst.MAXLO,
String.Format(FluentConst.WHERE_FMT_STR, "Profile"));
Map(x => x.Type).CustomType<int>().Column("ProfileType");
HasManyToMany(x => x.Profilables)
.ParentKeyColumn("ProfileID")
.ChildKeyColumn("EducationID")
.Cascade.All()
.Table("EducationProfile");
}
I am getting this error, and am wondering if anyone has any idea how to debug this.
Thanks
Initializing[GL.Objects.Profile`1[[GL.Objects.Education.Education,
GL.Objects, Version=0.1.1.0,
Culture=neutral,
PublicKeyToken=ebf25c7be4be0c91]]#289289]-failed to lazily initialize a collection of role:
GL.Objects.Profile`1[[GL.Objects.Education.Education,
GL.Objects, Version=0.1.1.0,
Culture=neutral,
PublicKeyToken=ebf25c7be4be0c91]].Profilables,
no session or session was closed"
Starts up the session
var watsonService = new WatsonService();
This code saves the instantiated objects.
watsonService.SaveEducation(e);
epf1.Profilables.Add(e);
watsonService.SaveEducationProfile(epf1);
epf2 = watsonService.GetEducationProfile(epf1.ID.Value);
The first assert passes just fine. epf2 has an id and is instantiated.
Assert.AreEqual(epf1.ID, epf2.ID);
This assert fails. The Profileables matches a composite table which id's get inserted successfully. But now when I try to access the first element in the collection it fails.
Assert.AreEqual(epf1.Profilables[0].ID, epf2.Profilables[0].ID);
This is the method implementation that get's called by the GetEducationProfile method.
public T Get<T>(int id) where T : IDataObject
{
return (T)_session.Load(typeof(T), id, LockMode.Read);
}
This is the definition of the constructor in the Fluent Mapping file.
public EducationProfileMap()
{
Table("Profile");
Id(x => x.ID)
.Column("ProfileID")
.GeneratedBy
.HiLo(FluentConst.HILO_TABLE,
FluentConst.NEXTHI_COLUMN,
FluentConst.MAXLO,
String.Format(FluentConst.WHERE_FMT_STR, "Profile"));
Map(x => x.Type).CustomType<int>().Column("ProfileType");
HasManyToMany(x => x.Profilables)
.ParentKeyColumn("ProfileID")
.ChildKeyColumn("EducationID")
.Cascade.All()
.Table("EducationProfile");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回时,会话将被丢弃,因此不会发生延迟加载。
我需要做的是确保在会话仍然打开时发生延迟加载。
On the return the session is thrown away so lazy loading can't happen.
What I need to do, is to make sure the lazy loading is happening while the session is still open.