I can not figure out why im getting session is closed when calling the second query in my class.
First i call the method, getPoliceData() I runs fine After that im calling GetSkadeData() and it throw the error:
the DbFactory class looks like this
class DbFactory
{
private static Lazy<ISessionFactory> factory = new Lazy<ISessionFactory>(GetSessionFactory, System.Threading.LazyThreadSafetyMode.ExecutionAndPublication);
public DbFactory()
{
}
public List<PoliceData> getPoliceData() {
using (ISession session = OpenSession())
{
IList<PoliceData> pols = session.Query<PoliceData>().Where(p => p.policyNumber == 053126703).ToList();
return pols.ToList();
}
}
public List<SkadeData> getSkadeData()
{
using (ISession session = OpenSession())
{
IList<SkadeData> skader = session.Query<SkadeData>().Where(p => p.Postnr == "7700").ToList();
return skader.ToList();
}
}
private static ISession OpenSession()
{
return factory.Value.GetCurrentSession();
}
private static ISessionFactory GetSessionFactory()
{
//NHibernate.Cfg.Configuration
var c = new Configuration();
//c.Configure();
c.DataBaseIntegration(db =>
{
db.ConnectionString = "Server=\"localhost\";database=testdb;Integrated Security=SSPI";
db.Dialect<NHibernate.Dialect.MsSql2012Dialect>();
});
//c.Configure("c:\XML.xml");
ModelMapper maps = new ModelMapper();
maps.AddMapping<PoliceDataMap>();
maps.AddMapping<SkadeDataMap>();
c.AddMapping(maps.CompileMappingForAllExplicitlyAddedEntities());
c.CurrentSessionContext<NHibernate.Context.ThreadLocalSessionContext>();
//c.Configure().Configure();
var sessionFac = c.BuildSessionFactory();
return sessionFac;
//return sessionFac.GetCurrentSession();
}
}
When I call the method from another class I am doing this
List<PoliceData> test = new List<PoliceData>();
List<SkadeData> skader = new List<SkadeData>();
DbFactory poli = new DbFactory();
test = poli.getPoliceData();
skader = poli.getSkadeData();
Do I need to create a new instance of dbfactory or is it posible to use the same session for two different querys. It would be nice if the Nhibernate just got confured once and after that you just open and closed session when needed.