Session is Closed! NHibernate shouldn't be trying to grab data
- by Jeremy Holovacs
I have a UnitOfWork/Service pattern where I populate my model using NHibernate before sending it to the view. For some reason I still get the YSOD, and I don't understand why the object collection is not already populated.
My controller method looks like this:
public ActionResult PendingRegistrations()
{
var model = new PendingRegistrationsModel();
using (var u = GetUnitOfWork())
{
model.Registrations = u.UserRegistrations.GetRegistrationsPendingAdminApproval();
}
return View(model);
}
The service/unit of work looks like this:
public partial class NHUserRegistrationRepository : IUserRegistrationRepository
{
public IEnumerable<UserRegistration> GetRegistrationsPendingAdminApproval()
{
var r =
from UserRegistration ur in _Session.Query<UserRegistration>()
where ur.Status == AccountRegistrationStatus.PendingAdminReview
select ur;
NHibernateUtil.Initialize(r);
return r;
}
}
What am I doing wrong?