(流畅)Nhibernate 延迟加载及其对 Parallel.ForEach 的影响

发布于 2024-09-13 09:05:48 字数 447 浏览 3 评论 0原文

当我(从数据库)请求实例时,我通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

帝王念 2024-09-20 09:05:48

我真的永远不会尝试这样做。每个 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文