Home > .net 2.0, .net 3.0, .net 3.5, c#, Microsoft, Oracle, Visual Studio 2008 > Using Oracle ODP with NHibernate From a C# Class Library

Using Oracle ODP with NHibernate From a C# Class Library

November 7, 2008

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 ?>

<hibernate-configuration xmlns=urn:nhibernate-configuration-2.2>

       <session-factory>

              <property name=connection.provider>

                     NHibernate.Connection.DriverConnectionProvider

              </property>

              <property name=dialect>

                     NHibernate.Dialect.Oracle9Dialect

              </property>

              <property name=connection.driver_class>

                     NHibernate.Driver.OracleDataClientDriver

              </property>

              <property name=connection.connection_string>

                     User Id=user;

                     Password=password;

                     Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=
                           (PROTOCOL=TCP)(HOST=server)(PORT=1521)))
                           (CONNECT_DATA=(SERVICE_NAME=service.site)));

                     Pooling=true;

                     Enlist=false;

                     Statement Cache Size=50;

                     Min Pool Size=10;

                     Incr Pool Size=5;

                     Decr Pool Size=2;

              </property>

              <property name=show_sql>

                     true

              </property>

              <mapping assembly=NHibernateDemo.Core/>

       </session-factory>

</hibernate-configuration>

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 v2.111.6.20 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).

<runtime>

       <assemblyBinding xmlns=urn:schemas-microsoft-com:asm.v1>

              <qualifyAssembly partialName=Oracle.DataAccess

                     fullName=Oracle.DataAccess,

                            Version=2.111.6.20,

                           Culture=neutral,

                           PublicKeyToken=89b483f429c47342 />

       </assemblyBinding>

</runtime>

Save, compile, run tests…

Successful XUnit Tests with NHibernate and Oracle ODP

Success!

About these ads
  1. July 23, 2009 at 12:22 am | #1

    what about this value NHibernate.Dialect.Oracle9Dialect for odp.net 11g release?

    • July 23, 2009 at 7:42 am | #2

      The Oracle9Dialect (or Oracle9iDialect if you’ve moved up to NHibernate 2.1) works just fine for Oracle 10g/11g. NHibernate 2.1 added Oracle10gDialect to add ANSI join support.

Comments are closed.
Follow

Get every new post delivered to your Inbox.

%d bloggers like this: