Book Image

NHibernate 4.x Cookbook - Second Edition

By : Gunnar Liljas, Alexander Zaytsev, Jason Dentler
Book Image

NHibernate 4.x Cookbook - Second Edition

By: Gunnar Liljas, Alexander Zaytsev, Jason Dentler

Overview of this book

NHibernate is a mature, flexible, scalable, and feature-complete open source project for data access. Although it sounds like an easy task to build and maintain database applications, it can be challenging to get beyond the basics and develop applications that meet your needs perfectly. NHibernate allows you to use plain SQL and stored procedures less and keep focus on your application logic instead. Learning the best practices for a NHibernate-based application will help you avoid problems and ensure that your project is a success. The book will take you from the absolute basics of NHibernate through to its most advanced features, showing you how to take full advantage of each concept to quickly create amazing database applications. You will learn several techniques for each of the four core NHibernate tasks—configuration, mapping, session and transaction management, and querying—and which techniques fit best with various types of applications. In short, you will be able to build an application using NHibernate by the end of the book. You will also learn how to best implement enterprise application architecture patterns using NHibernate, leading to clean, easy-to-understand code and increased productivity. In addition to new features, you will learn creative ways to extend the NHibernate core, as well as gaining techniques to work with the NHibernate search, shards, spatial, envers, and validation projects.
Table of Contents (17 chapters)
NHibernate 4.x Cookbook Second Edition
Credits
About the Authors
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Index

Using NHibernate schema tool


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, we'll show you how to use this command-line tool to run our hbm2ddl tasks.

Getting ready

Download the latest release of NHibernate Schema Tool from http://nst.codeplex.com/.

To install NHibernate Schema Tool, follow these steps:

  1. Create a new folder in C:\Program Files named NHibernateSchemaTool.

  2. Copy nst.exe to the newly created folder.

  3. Add C:\Program Files\NHibernateSchemaTool to your PATH environment variable.

  4. Complete the Configuring NHibernate with hibernate.cfg.xml recipe from the beginning of this chapter.

Note

This recipe works for any RDBMS supported by NHibernate. To use a different system, adjust your connection string and dialect accordingly.

How to do it...

  1. Build your solution.

  2. Open a command prompt window, and switch to the directory containing your compiled mapping assembly and hibernate.cfg.xml.

    Note

    To open the command prompt window quickly, in Visual Studio, right-click on your project, and choose Open Folder in Windows Explorer. Open the bin folder. While holding down Shift, right-click on the Debug folder. Choose Open Command Window Here.

  3. Run the following command:

    nst /c:hibernate.cfg.xml /a:Eg
    .Core.dll /o:Create.
    

We haven't added any HBM mapping files to the Eg.Core project yet, so no tables will be created. In the next chapter, however, we will go into some depth on how these mappings are created.

How it works...

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. 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.

There's more...

NST has several options, enabling a number of creative uses. NST supports these command-line options:

Command-line option

Description

/c:<path-to-hibernate-config>

Specifies NHibernate config file to use.

/a:<assembly[;assembly2]>

Path to assembly or semicolon-separated list of assemblies containing embedded .hbm.xml files. These assemblies may also contain persistent classes.

/m:<assembly[;assembly2]>

Path to assembly or semicolon-separated list of assemblies containing persistent classes.

/d:<path[;path2]>

Directory or directories containing .hbm.xml mapping files.

/s

Generate script, but don't execute. Script is written to the console.

/v

Generate script and execute. Script is written to the console.

/o:<Create|Update|Delete>

Specifies the Create, Update, or Delete operation.

See also

  • Configuring NHibernate with App.confiig or Web.config

  • Configuring NHibernate with hibernate.cfg.xml

  • Configuring NHibernate with code

  • Configuring NHibernate with Fluent NHibernate

  • Generating the database

  • Scripting the database