Sqlite Database Creation

arkhan4's Avatar

arkhan4

31 Mar, 2010 02:15 AM via web

Hi,
I'm new at using Fluent Hibernate and databases. So to get familiar with working with databases I started a learning project of mine to learn to work with them.

So far I'm at the point where I have mappings setup, not using the Automapping functionality. I have unit tests working, using an in memory sqlite database. I'm at the point where I tried to create the objects and save them to the database, but then I soon realized I needed a database to save it to.

So my main question is how can I create a new sqlite3 database file from my fluent nhibernate mappings at runtime? Is that possible or am I misunderstanding something?

Thanks

  1. Support Staff 2 Posted by Paul Batum on 31 Mar, 2010 03:33 AM

    Paul Batum's Avatar

    If you want to move from using an in memory db to a file based db, its pretty straightforward. You want:

     SQLiteConfiguration.Standard
       .UsingFile("database.db");
    

    See: http://wiki.fluentnhibernate.org/Database_configuration

  2. 3 Posted by arkhan4 on 31 Mar, 2010 12:50 PM

    arkhan4's Avatar

    I did try that, but I don't have the database file created yet? Are you saying it will create it for me automatically if not there?

    I tried using ExportSchema, but I don't understand what it's going to export, a new database file with tables based on my mappings or sqlite sql instructions to create the tables based on the mappings?

    Here's the code I have so far related to creating a SessionFactory:
    private static ISessionFactory m_sessionFactory;

      public static ISessionFactory SessionFactory
      {
         get
         {
            if(m_sessionFactory == null)
            {
               m_sessionFactory = CreateSessionFactory();
            }
    
            return m_sessionFactory;
         }
      }
    
      private static ISessionFactory CreateSessionFactory()
      {
         //Database File Location
         Utilities.DB_File = Application.StartupPath + Properties.Settings.Default.DB_File;
    
         ISessionFactory sessionFactory = Fluently.Configure().Database(SQLiteConfiguration.Standard.UsingFile(Utilities.DB_File)).
                                                               Mappings(m =>
                                                                {
                                                                   m.FluentMappings.AddFromAssemblyOf<TestMapping>();
                                                                }).
                                                               ExposeConfiguration(VerifyDbFile).
                                                               BuildSessionFactory();
    
         return sessionFactory;
      }
    
      private static void VerifyDbFile(Configuration config)
      {
         if (!File.Exists(Utilities.DB_File))
         {
            String directory = Utilities.DB_File.Substring(0, Utilities.DB_File.LastIndexOf("\\"));
            if(!Directory.Exists(directory))
            {
               Directory.CreateDirectory(directory);
            }
    
            new SchemaExport(config).Create(false, true);
         }
      }
    
  3. Support Staff 4 Posted by Paul Batum on 25 Apr, 2010 02:00 PM

    Paul Batum's Avatar

    Sorry, somehow I lost track of this conversation. You've probably already resolved this but in case you're still stuck...

    I forget exactly what the parameters are for the Create method on SchemaExport, but my little test FNH project that I use to diagnose issues passes (true, true).

    See here:
    http://github.com/paulbatum/Fluent-NH-Test-Bed/blob/master/Console/...

    You can download the entire test project from github if you need a working example:
    http://github.com/paulbatum/Fluent-NH-Test-Bed/archives/master

  4. Paul Batum closed this discussion on 16 May, 2010 06:51 AM.

Comments are currently closed for this discussion. You can start a new one.