Here is the helper;
public class NHibernateHelper
{
private static ISessionFactory _sessionFactory;
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
InitSessionFactory();
return _sessionFactory;
}
}
private static void InitSessionFactory()
{
_sessionFactory =
Fluently.Configure().Database(
MsSqlConfiguration.MsSql2008.ConnectionString(
"ConnectionString").ShowSql).
Mappings(m => m.FluentMappings.AddFromAssemblyOf<Category>()).ExposeConfiguration(
cfg => new SchemaExport(cfg).Create(true, true)).BuildSessionFactory();
}
public static ISession OpenSession()
{
return SessionFactory.OpenSession();
}
}
Below code is the code to store data:
using(var session = NHibernateHelper.OpenSession())
{
using (var tx = session.BeginTransaction())
{
session.Save(category);
tx.Commit();
}
}
My problem is when i run this code and i refresh database, i see data is going through.
When i restart the application, data is gone.
How can i persist the data to db?
It looks as if you are constantly re-creating the database file:
SchemaExport(cfg).Create(As asked and solved here:
How do I configure FluentNHibernate to not overwrite an existing SQLite db file?
So it is persisting within an app session, but once you restart the app (or more accurately call
SchemaExportagain) the database file is re-created.