Michaël Larouche
Michaël Larouche

Reputation: 847

Best practices to generate schema for production with NHibernate

I am now ready to use NHibernate to persist my data layer access. I used DDD until that point and I use fake for unit tests and for the test site.

I know that I can use SchemaExport for unit/integration tests of my NHibernate concrete repositories but how should I generate the schema to be used for the test site ?

Should I create a special class in my tests that create the schema and insert the static data or should I generate the schema at the launch of the site if the database is not created ?

Upvotes: 1

Views: 699

Answers (3)

Jon Spokes
Jon Spokes

Reputation: 2599

For your full build script I'd go with Markus's suggestion, for just running your unit tests I'd put

<property name="hbm2ddl.auto">create-drop</property>

in the app config of you test project - this will drop and recreate your schema everytime all the tests are run. Each unit test can add the data it needs to test.

Upvotes: 0

Markus Dulghier
Markus Dulghier

Reputation: 1580

Personally I like to use a small console application which loads all the mappings and executes a SchemaExport as below:

new SchemaExport(config).Execute(ddlScript => {
    using (var writer = new StreamWriter(fileName, true))
    {
        writer.Write(ddlScript);
        writer.Flush();
    }
}, false, false);

This console app runs as a step of the build script and the DDL script file then gets picked up by WiX and is included in the MSI package (which generates the whole database at install time).

Upvotes: 3

Michal
Michal

Reputation: 3169

As a simple scenario you can miss use a unit test for that. Just create unit test called CreateSchema which will do the schemaexport. Then run it before you will run the other tests.

I have a similar question here on STO

Upvotes: 0

Related Questions