Configure NHibernate With Embedded Resource[source]
xml
<glacius:metadata> | |
<title>Configure NHibernate With Embedded Resource</title> | |
<description>How to configure NHibernate for a test assembly circa 2010</description> | |
<category>C#</category> | |
<category>Programming</category> | |
</glacius:metadata> | |
<glacius:macro name="legacy blargh banner"> | |
<properties> | |
<originalUrl>https://tmont.com/blargh/2010/11/configure-nhibernate-with-embedded-resource</originalUrl> | |
<originalDate>2010-11-09T04:47:05.000Z</originalDate> | |
</properties> | |
</glacius:macro> | |
<p> | |
I spent a couple dozen minutes figuring this out today. There are several different ways to | |
configure NHibernate: | |
</p> | |
<ol> | |
<li> | |
The default way, which is having a <code>hibernate-section</code> configuration section in | |
your app.config, or having a <code>hibernate.cfg.xml</code> in the root of your app. | |
</li> | |
<li>Embedded resource</li> | |
<li>Hard-coded path to config file</li> | |
</ol> | |
<p> | |
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 <code>hibernate.cfg.xml</code> | |
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. | |
</p> | |
<p> | |
So, I left the <code>hibernate.cfg.xml</code> file in my non-root directory, and turned it | |
into an embedded resource. | |
</p> | |
<glacius:figure glacius:src="hibernate-config-properties.png"> | |
<caption>hibernate.cfg.xml properties config</caption> | |
</glacius:figure> | |
<p> | |
Now you have to figure out the manifest resource name of this embedded resource, which is just | |
the path to the file on disk. | |
</p> | |
<glacius:code lang="csharp"><![CDATA[ | |
// 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" | |
);]]></glacius:code> | |
<p>And then you profit.</p> |