Folks,
I'm adding this for my future reference as much as anyone elses :-).
I've spent about 2 days trying to get nhibernate to use app.config. I had previously given up but I really need to use it for my current project so I had to dig deep.
First off, keep app.config (not appconfig.xml as some posts suggest). Also leave it as Content and not an Embedded Resource (just in case you thought otherwise).
My app.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="hibernate-configuration" type="NHibernate.Cfg.ConfigurationSectionHandler, NHibernate"/>
</configSections>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
<session-factory>
<property name="dialect">NHibernate.Dialect.OracleDialect</property>
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.connection_string">Data Source=XXXXXX;User Id=AAAAAAAA;Password=BBBBBB;Integrated Security=no;</property>
</session-factory>
</hibernate-configuration>
</configuration>
Things to note in the above:
1. Property name fields don't have 'nhibernate.' before them etc.
2. '<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">' is the section name. It must have this label try something else and you'll get an 'An exception occurred during configuration of persistence layer.' exception.
How to initialise:
Configuration cfg = new Configuration();
// The next line actually reads from the app.config/hibernate.cfg.xml
// but not sure how it decides. Perhaps goes for app.config and then
// hibernate.cfg.xml as the last resort?
cfg.Configure();
// Add the assembly that contains the mapping xml file (needs your class for the objects to be configured correctly).
// Note: The XML file is an embedded resource in the project.
cfg.AddAssembly("Mappings");
Hope that makes sense!!
John
|