Green Dragon's Lair

Invalid attempt to MetaData when reader is closed - Solved

I'm working with NHibernate and ActiveRecord, and a few months ago I started getting InvalidOperationException - Invalid attempt to MetaData when reader is closed whenever I'd run queries inside a SessionScope.

It took me a bit of time, but I finally resolved this problem down to statically initiallizing fields that access the database through AR, so if you have in your class something like this:
public class X
{
SomeEntity entity = Find(2);
X() {...}
}
you'll get the exception while loading it inside a session scope.
The solution would be to move the initialization to a non-default constructor, something like this:
public class X
{
SomeEntity entity;
X() {//do not initialize here }
X(...) { entity = Find(2); }
}


Posted Apr 28 2008, 11:06 AM by admin

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
(C) Miki Watts
Powered by Community Server (Non-Commercial Edition), by Telligent Systems