(流畅)Nhibernate 延迟加载及其对 Parallel.ForEach 的影响
当我(从数据库)请求实例时,我通过 FNH 配置了多个实体,以使用 FetchMode.Eager 语法预先加载子实体。现在我的印象是,这会忽略映射中的任何延迟加载,并用“真实”数据填充子实体。
我想要这样做的原因是因为我想使用 Parallel.ForEach 来迭代实体集合并生成一组结果,但我收到以下错误:
[18000] System.InvalidOperationException:
There is already an open DataReader associated with this Command
which must be closed first.
如果我使用 ' NHibernateUtil.Initialize' 初始化所有子实体,然后它按预期工作。
我对使用FetchMode.Eager
的理解是否错误?
I have several entities configured via FNH to eager load child entities using the FetchMode.Eager
syntax when I request instances (from the database). Now I was under the impression this would ignore any lazy loading in the mapping and populate the child entities with the 'real' data.
The reason why I want to do this is because I want to use a Parallel.ForEach
to iterate over a collection of entities and generate a set of results, but I get the following error:
[18000] System.InvalidOperationException:
There is already an open DataReader associated with this Command
which must be closed first.
If I use ' NHibernateUtil.Initialize' to initialise all child entities then it works as expected.
Am I wrong in my understanding in the use of FetchMode.Eager
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我真的永远不会尝试这样做。每个 Parallel.ForEach 函数都可以在不同的线程上运行。根据您的配置,NHibernate ISession 存储在一个线程中,这意味着除了调用线程之外的任何其他线程都无法访问该会话,这就是整个事情失败的原因。
就我个人而言,我会将整个内容检索到传输对象中,然后对其执行 Parallel.ForEach 。
I really would not ever try and do that. Each Parallel.ForEach func can run on a different thread. Depending on your configuration, the NHibernate ISession is stored against a thread meaning anything other than the calling thread cannot access the session which is why the whole thing fails.
Personally I'd retrieve the whole thing into a transfer object then do the Parallel.ForEach on that.