C# NHibernate.LazyInitializationException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1125988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NHibernate.LazyInitializationException
提问by Josh
We have been having this issue pop up sporadically, but now I can reproduce it every time. I am incrementing a view counter on my custom built forums, which causes an error:
我们偶尔会遇到这个问题,但现在我每次都可以重现它。我在自定义构建的论坛上增加了一个查看计数器,这会导致错误:
NHibernate.LazyInitializationException: failed to lazily initialize a collection, no session or session was closed
NHibernate.LazyInitializationException:无法延迟初始化集合,没有会话或会话被关闭
This error occurs on another collection in the object. If I add:
此错误发生在对象中的另一个集合上。如果我添加:
.Not.LazyLoad()
.Not.LazyLoad()
To my Fluent mapping, the error shifts around my project. I kept disabling lazy loading on objects intil it going to a spot where there was no lazy loading, and then it threw this error:
对于我的 Fluent 映射,错误会围绕我的项目转移。我一直禁用对象上的延迟加载,直到它转到没有延迟加载的位置,然后它抛出了这个错误:
NHibernate.LazyInitializationException: Could not initialize proxy - no Session.
NHibernate.LazyInitializationException: 无法初始化代理 - 没有会话。
So, then I took out the nots on my lazy loading and now I'm back to square one. It only errors when I increment this view counter. Here is a snippet of my base class save code:
所以,然后我在我的懒加载中去掉了nots,现在我回到了原点。只有当我增加这个视图计数器时才会出错。这是我的基类保存代码的片段:
using (ISession session = GetSession())
using (ITransaction tx = session.BeginTransaction())
{
session.SaveOrUpdate(entity);
tx.Commit();
}
Looking around, I read in another post that the transactions can cause an issue, but that was because of where they were placed. This code is extended to classes that are separate from my domain objects (repository classes). Here is the post:
环顾四周,我在另一篇文章中读到交易可能会导致问题,但那是因为它们的放置位置。此代码扩展到与我的域对象(存储库类)分开的类。这是帖子:
hibernate: LazyInitializationException: could not initialize proxy
休眠:LazyInitializationException:无法初始化代理
I don't believe that is my issue here. Here is my fluent mapping for the first collection that is throwing the error. There are several other similar collections.
我不相信这是我的问题。这是我对抛出错误的第一个集合的流畅映射。还有其他几个类似的集合。
HasManyToMany(x => x.Votes)
.WithTableName("PostVotes")
.WithParentKeyColumn("PostId")
.WithChildKeyColumn("VoteId");
采纳答案by Josh
A little further research into this issue as it has reproduced itself is that in a single call back to the server, if you do a save and get, you need to flush the session. I do my flush after the save and that seems to have corrected the problem.
对这个问题的进一步研究,因为它已经重现了,在对服务器的一次回调中,如果你进行了保存和获取,你需要刷新会话。我在保存后进行冲洗,这似乎解决了问题。
回答by Andrew Hanson
Check to see that there aren't any other objects loaded in that session. I've had a similar situation occur in which I would call Save on an object that didn't have any Lazy Loading and it really sent me for a loop. Why would I get this error on an object that isn't lazy loading?
检查以查看该会话中没有加载任何其他对象。我遇到过类似的情况,我会在一个没有任何延迟加载的对象上调用 Save 并且它真的让我循环。为什么我会在非延迟加载的对象上收到此错误?
In my situation I was loading several other objects into the page and these object weren't mapped properly. When I called Save NHibernate would try to synchronize in the session and throw the error.
在我的情况下,我正在将其他几个对象加载到页面中,但这些对象没有正确映射。当我调用 Save NHibernate 会尝试在会话中同步并抛出错误。
Try removing all other NHibernate, leaving just this counter update. If the error doesn't occur with just the counter slowly add back in you other calls until the error returns. Then you can start addressing the real culprit.
尝试删除所有其他 NHibernate,只留下这个计数器更新。如果错误没有发生,只是计数器慢慢地添加回你的其他调用,直到错误返回。然后你就可以开始解决真正的罪魁祸首了。