0
我需要加载为延迟加载设置的对象的属性。当我尝试访问它或使用NhibernateUtil.Initialize()加载时,我得到相同的错误:加载懒惰代理属性
“正在初始化[ProjectName.Logic.Entities.AddressList#9] - 无法初始化代理 - 没有会话。”
我可以通过调用必要的方法在“using”子句中打开会话来确保会话确实存在。 (我们已经隐藏了我们的会话创建,使得使用参数“true”实例化存储库对象也将创建会话工厂,并根据需要创建会话工厂并打开会话。使用在“using”子句中触发的断点进行验证。)
foreach (MemberViewModel MVM in _filteredMemberViewModels)
{
foreach (Detail Mailings in MVM.Member.Mailings)
{
//used for lazy loading
using (var repo = new AddressListRepository(true))
{
NHibernateUtil.Initialize(Mailings.AddressList);
}
}
}
详细映射:
public class DetailMap : ClassMap<Detail>
{
public DetailMap()
{
Table("AddressDetailsCCN");
// Unique Identifier
Id(x => x.Id, "Id")
.GeneratedBy.Identity();
// MANY TO ONE relationship (the list has many details)
References<AddressList>(x => x.AddressList, "ListId")
.LazyLoad()
.Not.Nullable()
.Cascade.None();
// MANY TO ONE relationship (Members have details)
References<Member>(x => x.Member, "MemberId")
.Not.LazyLoad()
.Not.Nullable();
// First line of Address
Map(x => x.Address, "Address")
.Nullable();
// Second line of Address
Map(x => x.Address2, "Address2")
.Nullable();
// City
Map(x => x.City, "City")
.Nullable();
// State
Map(x => x.State, "State")
.Nullable();
// Zip
Map(x => x.Zip, "Zip")
.Nullable();
// Finalized date
Map(x => x.FinalizedDate, "FinalizedDate")
.CustomType(typeof(DateTime))
.Nullable();
// Date the list is created by
Map(x => x.CreatedDate, "CreatedDate")
.CustomType(typeof(DateTime))
.Not.Nullable();
}
}
当你来访问懒惰的加载项目时,你确定会话仍然打开吗?如果您在using语句结束后访问延迟加载的项目(即在使用方法中),那么会话可能已经关闭。 –