Monday, September 7, 2009

Using NHibernate ActiveRecord and LINQ

I'm trying to learn NHibernate ActiveRecord and LINQ all in the same day. I ran into the following exception:
Session is closed!
Object name: 'ISession'.

When trying to execute the following code:
      public static User FindByUserName(string username)
{
// Note that we use the property name, _not_ the column name
return (from p in User.Queryable
where p.Username == username
select p).First();
}

It took me a couple hours to get by this one, so I thought I would post it. I had to wrap it in a using (new SessionScope()) statement. Like this:
       public static User FindByUserName(string username)
{
using (new SessionScope())
{
// Note that we use the property name, _not_ the column name
return (from p in User.Queryable
where p.Username == username
select p).First();
}
}


Happy Coding.

No comments:

Post a Comment