The easiest way to generate your schema from your mappings is to use the NHibernate SchemaExport tool.
You can tie this in to the Fluent Configuration API using it's ExposeConfiguration
method, which lets you deal with the actual NHibernate Configuration
that SchemaExport
requires.
Here's a basic example:
Fluently.Configure() .Database(SQLiteConfiguration.Standard.InMemory) .Mappings(/* your mappings */) .ExposeConfiguration(cfg => { new SchemaExport(cfg) .Create(false, true); }) .BuildSessionFactory();
The ExposeConfiguration
method will be called after all the mappings have been added to the Configuration
instance, so it can then be used by the SchemaExport
tool.
This could be refactored slightly using C#'s syntactic sugar for lambdas:
Fluently.Configure() .Database(SQLiteConfiguration.Standard.InMemory) .Mappings(/* your mappings */) .ExposeConfiguration(BuildSchema) .BuildSessionFactory();
Where BuildSchema
could be defined elsewhere as:
private void BuildSchema(Configuration cfg) { new SchemaExport(cfg) .Create(false, true); }