Configure NHibernate With Embedded Resource

This article was originally published in my blog (affectionately referred to as blargh) on . The original blog no longer exists as I've migrated everything to this wiki.

The original URL of this post was at Hopefully that link redirects back to this page.

I spent a couple dozen minutes figuring this out today. There are several different ways to configure NHibernate:

  1. The default way, which is having a hibernate-section configuration section in your app.config, or having a hibernate.cfg.xml in the root of your app.
  2. Embedded resource
  3. Hard-coded path to config file

In my app, I have some tests that spin up a database and test out some NHibernate-related stuff, like that things cascade properly. So that means I needed a hibernate.cfg.xml file for my test assembly as well. And that sucked, because I didn't want to put it at the root of the application because it cluttered up the directory and I have a disorder regarding stuff like that.

So, I left the hibernate.cfg.xml file in my non-root directory, and turned it into an embedded resource.

hibernate-config-properties.png (image/png, 302x244, 7,154 bytes)
hibernate.cfg.xml properties config

Now you have to figure out the manifest resource name of this embedded resource, which is just the path to the file on disk.

// assuming this class and hibernate.cfg.xml reside in the same directory
new NHibernate.Cfg.Configuration().Configure(

And then you profit.