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 https://tmont.com/blargh/2010/11/configure-nhibernate-with-embedded-resource. 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:
The default way, which is having a
hibernate-sectionconfiguration section in your app.config, or having a
hibernate.cfg.xmlin the root of your app.
- Embedded resource
- 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
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
So, I left the
hibernate.cfg.xml file in my non-root directory, and turned it
into an embedded resource.
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( GetType().Assembly, "My.Sweet.App.NotTheRootDirectory.hibernate.cfg.xml" );
And then you profit.