Wednesday, July 1, 2009

NHibernate Schema Export

In my experience developers normally fit into two categories in relation to developing applications that interact with a database. 

Data Up - Build the database, then work from up from the schema to relate domain objects and their relationships

Domain Down - Build the domain model, then work down to the schema ensuring the data from the domain is persisted.

Both have their pros & cons.  NHibernate is just one tool that helps fill the void no matter which type the developer is. However there is one feature of this tool that I find useful and saves a lot of time

Using the code example (Examples.FirstProject) from the Fluent NHibernate source we have the following domain.

Domain

By using the fluent configuration code below we can build up the mappings and export the schema (check the mapping files in the Examples.FirstProject)

Configuration configuration
    = Fluently.Configure()
        .Database
            (MsSqlConfiguration.MsSql2005
                .ConnectionString(conn => conn.FromConnectionStringWithKey("ExampleConnectionString")))
        .Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
        .BuildConfiguration();

new SchemaExport(configuration).Create(true, true);

When this is run, the following schema is created

Schema

Guess which type of developer I am.

Get Amongst It!!!

0 comments: