I currently have a NHibernateHelper class which creates a session factory. I keep recieving errors saying 'Session Closed!'. Am I going about this in the wrong way? The error occurs when I call the Add(Login login) which gets called after Add(User user)
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly("System.Core");
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
Here is my repository:
internal class UserRepository : IUserRepository
{
private ISession _db = NHibernateHelper.OpenSession();
public void Add(User user)
{
using (_db)
{
using (ITransaction transaction = _db.BeginTransaction())
{
IEnumerable<UserRole> userRoles = user.UserRoles;
user.UserRoles = null;
_db.Save(user);
foreach (UserRole userRole in userRoles)
{
userRole.UserID = user.UserID;
_db.Save(userRole);
}
transaction.Commit();
}
}
}
public void Add(Login login)
{
using (_db)
{
using (ITransaction transaction = _db.BeginTransaction())
{
_db.Save(login);
transaction.Commit();
}
}
}
}