C# – 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 ?

Best Solution

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))
}, 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).

Related Question