Using Oracle ODP with NHibernate From a C# Class Library
I’m working through a few demos today and needed to connect to an Oracle 10g database using NHibernate. NHibernate, out of the box, has a Dialect for 10g/11g using the Oracle9Dialect; however, relies on Microsoft’s OracleClient library. I prefer Oracle’s own ODP and, thankfully, after a few configuration settings, NHibernate complies. 🙂
On the library with your mappings and domain classes, be sure that the hibernate.cfg.xml is set to Build Action: “Content” and Copy to Output Directory: “Copy always” when included in a class library (so it’s available in the /bin directory for the other consuming projects).
<?xml version=“1.0“ encoding=“utf-8“ ?>
Statement Cache Size=50;
Min Pool Size=10;
Incr Pool Size=5;
Decr Pool Size=2;
The connection.driver_class property value of NHibernate.Driver.OracleDataClientDriver tells NHibernate to look for the ODP (thanks Tomer!). After wards, add in Oracle.DataAcess into the references of the project. Unfortunately, there’s one more step.
If you try to compile and run tests at this point, you’ll get an NHibernate.HibernateException:
NHibernate.HibernateException : The IDbCommand and IDbConnection implementation in the assembly
Oracle.DataAccess could not be found. Ensure that the assembly Oracle.DataAccess is located
in the application directory or in the Global Assembly Cache. If the assembly
is in the GAC, use <qualifyAssembly/> element in the application configuration file to
specify the full name of the assembly.
The qualifyAssembly key goes in the App.config/Web.config file; however, I cannot get this to work using an App.config within the library itself. For example, you’d need this qualifyAssembly key in EVERY application (tests, etc) that called your NHibernate Oracle objects. Not cool.
For me, I’m using v126.96.36.199 of the ODP.NET for 11g. Since I can’t get this working inside the class library, I’ll create and place it in the App.config of my Unit test project (demos with unit tests == good, console applications == bad).
Save, compile, run tests…