2

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?

AwkwardCoder
  • 24,893
  • 27
  • 82
  • 152

1 Answers1

1

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.

Deleted
  • 4,804
  • 1
  • 22
  • 17
  • i know all that, hence the use of 'NHibernateUtil.Initialize' – AwkwardCoder Mar 28 '11 at 09:05
  • It doesn't matter. Your objects still have proxies around them which expect context to be present, which it isn't if you are on a different thread. – Deleted Mar 28 '11 at 09:22
  • No it doesn't. I just tried it on NH3 and Castle proxy. It just loads the object. It would be illogical to not load the proxy in case there are any other aggregates on that object which need to be initialized etc... – Deleted Mar 28 '11 at 20:16