Search code examples
c#sql-servernhibernatefluent-nhibernatemdf

Fluent NHibernate local mdf file doesn't keep table structure


i have set up my database with Fluent NHibernate and SQL Server, and it was working fine with SQL Server. Everytime I execute the code, the database structure was created and data inserted throw the application interface was working perfectly. If I shut the program, the database and the content would stay saved.

I don't want the database in the SQL server, I want the database to be a *.mdf file in the solution file structure. So i can just copy the application without installation process.

The problem, and the reason of this topic, is that using the file in the solution file structure, the application works perfectly while running, but when i close the application the data is all gone. If i start the application again, the data I've inserted is gone.

My class is the following for the SQL Server:

class NHibernateHelper
{
    private static ISessionFactory _sessionFactory;

    private static ISessionFactory SessionFactory
    {
        get
        {
            if (_sessionFactory == null)
                InitializeSessionFactory();

            return _sessionFactory;
        }
    }

    private static void InitializeSessionFactory()
    {
        _sessionFactory = Fluently.Configure()
            .Database(
                MsSqlConfiguration.MsSql2012
                .ConnectionString(@"server=MyComputer;Database=MyDatabase;Trusted_Connection=true;")
                .ShowSql())
            .Mappings(m => m.FluentMappings.AddFromAssemblyOf<MyEntity>())
            .ExposeConfiguration(cfg => new SchemaUpdate(cfg).Execute(true, true))
            .BuildSessionFactory();
    }

    public static ISession OpenSession()
    {
        return SessionFactory.OpenSession();
    }
}

For the local file, i just change the ConnectionString line:

.ConnectionString(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="+AppDomain.CurrentDomain.BaseDirectory+ "General\\Data\\MyDatabase.mdf" + ";Integrated Security=True;")

To commit/persist data to the database I use the following structure:

        using (var session = NHibernateHelper.OpenSession())
        {
            using (var transaction = session.BeginTransaction())
            {

                var newEntity = new EntEntity
                {
                    //Data to populate

                };

                session.SaveOrUpdate(newEntity);
                transaction.Commit();

                if (transaction.WasCommitted)
                {
                    MessageAndCleanInterface("Sucessful created!");
                }
            }
        }

Does anyone knows why this happens? At this moment, i don't mind to use another type of database type, I will start to make tests with other types. Nevertheless, I would like to know the reason for this.


Solution

  • Visual Studio overwrites your .mdf with every build. If you start only your .exe file without building new, your data should be persisted.