In many cases, you'll want to include building or updating your database in some larger process, such as a build script or installation process. In this recipe, I'll show you how to use this command-line tool to run our hbm2ddl tasks.
Download the latest release of NHibernate Schema Tool from http://nst.codeplex.com/.
To install NHibernate Schema Tool, follow these steps:
C:Program Files
named NHibernateSchemaTool
.nst.exe
to the newly created folder.C:Program FilesNHibernateSchemaTool
to your PATH
environment variable.After the installation of the NHibernate Schema Tool, follow these steps:
NHCookbook
.hibernate.cfg.xml
with the following code:<?xml version="1.0" encoding="utf-8" ?> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2"> <session-factory> <property name="proxyfactory.factory_class"> NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle </property> <property name="dialect"> NHibernate.Dialect.MsSql2008Dialect, NHibernate </property> <property name="connection.connection_string"> Server=.SQLExpress; Database=NHCookbook; Trusted_Connection=SSPI </property> </session-factory> </hibernate-configuration>
hibernate.cfg.xml
, on the properties tab, set Copy To Output Directory to Copy Always.hibernate.cfg.xml
.nst /c:hibernate.cfg.xml /a:Eg.Core.dll /o:Create.
NHibernate Schema Tool is a command-line wrapper for the hbm2ddl tool. This makes NST ideal for use in build scripts and continuous integration servers.
The /c
argument specifies the configuration file. It's no mistake that the content of hibernate.cfg.xml
is nearly identical to the hibernate-configuration
section in the app.config
. The /a
argument specifies the assembly with our classes and mapping embedded resource files. The /o:Create
option tells NHibernate to create our database objects. It also supports Update
and Delete
.
NST has several options, enabling a number of creative uses. NST supports these command-line options: