I'm using NHibernate with FluentNHibernate for my DAL. I'm also using SchemaExport and/or SchemaUpdate to produce increase my database schema.
My problem would be that the schema procedures all require database to exist before they are effective. I wish to programmatically create my database increase the schema because there might be multiple databases and also the creating from the database isn't just a once-off operation.
I understand I'm able to do that by hand by performing a create database command on the link with the actual database, but this feels wrong thinking about which i otherwise use NHibernate for those my database interaction. (being an aside, I personally use a SQLite inmemory database for my unit tests so any sql I write will need to know which database I am using).
Can there be any method of getting HNibernate to produce my database for me personally?
Thanks
Andrew

No, NHibernate won't produce the database for you personally. Creating databases is one thing you typically do not want do be achieved quickly. It requires many configuration parameters which is very database specific, even database version specific.

For the product, I authored a category that creates the database. It's employed for the installer, the integration tests and also the database maintenance tool.

  • NHibernate produces the bond having its configuration.
  • my very own implementation produces the database. You will find different implementations for various dialects.
  • NH produces the tables along with other database objects
  • Installer classes create initial data

There's additionally a special situation, in which a customer does not wish to grant the authority to create databases. Only then do we expect him to get it done and skip this part.

We use SchemaExport use a schema along with a Nant task to decrease and recreate the database. Another Nant task runs the schema export and dataloading code which is available as an integration test run through the nunit task. There's a sample here.

I believe that producing db with NHibernate is great, should you begin to develop application with domain model. I am talking about - it gives a look the way your db need to look. However in general - sql scripts ought to be used.

I heard that some designers uses NHibernate db generation for integration tests, they've created lightweight sqlite db on fly.

How you can generate db with NHibernate? This is the way i actually do that:

 public static void InitSessionFactory()
    {
        var connectionString =
            ConfigurationManager
                .ConnectionStrings["MyConnectionString"]
                .ConnectionString;

        var cfgFromXml = new Configuration();
        cfgFromXml.Configure();

        var cfg = Fluently.Configure(cfgFromXml)
            .Database(MsSqlConfiguration.MsSql2005
                          .ConnectionString(x => x.Is(connectionString))
                          .UseReflectionOptimizer())
            .Mappings(x => x.FluentMappings
                               .AddFromAssemblyOf<NHibernateBootstrapper>())
            .ExposeConfiguration(BuildSchema);


        ObjectFactory.Inject(cfg.BuildSessionFactory());
    }

    private static void BuildSchema(Configuration config)
    {
        //Creates database structure
        //new SchemaExport(config)
        //   .Create(false, true);
    }

Hmm, you are able to indeed use SchemaExport while you mention, however i see NHibernate primarely in an effort to bridge the space between OO domain model along with a relational persistence storage. That's, for me personally NHibernate's purpose would be to persist and retrieve organizations during my domain model.

When for you to do schema updates, imho, you shouldn't use NHibernate (if at all possible whatsoever), since that isn't the objective of NHibernate there's a 'domain specific language' known as SQL :) that is proficient at doing individuals things.

To be able to create my database, increase my database-schema, I personally use Migrator.Internet, the industry framework which allows you are writing C# classes in in which you specify what must be done (produce a table, give a column, etc...).
The benefit of this, is the fact that Migrator.Internet will also support multiple DBMS'es, so you don't finish up writing DB-specific SQL code.