NHibernate 3.0 configuration with ODP.NET

I just installed NHibernate for the first time. My platform settings and configuration:

  • Database: Oracle 11.1g
  • ODP.NET Version: 4.112.1.2 (installed from ODTWithODAC112012, which is version 1 above my Oracle db installation)
  • NHibernate Version 3.0

I created a test MVC application with a test project. Then, to test the NHibernate connection, I use the following test equipment:

using IBCService.Models; using NHibernate.Cfg; using NHibernate.Tool.hbm2ddl; using NUnit.Framework; namespace IBCService.Tests { [TestFixture] public class GenerateSchema_Fixture { [Test] public void Can_generate_schema() { var cfg = new Configuration(); cfg.Configure(); cfg.AddAssembly(typeof(Product).Assembly); new SchemaExport(cfg).Execute(false, true, false); } } 

Nhibernate configuration file:

 <?xml version="1.0" encoding="utf-8"?> <!-- This config use Oracle Data Provider (ODP.NET) --> <hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" > <session-factory name="NHibernate.Test"> <property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property> <property name="connection.connection_string"> User ID=TEST;Password=******;Data Source=//RAND </property> <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property> <property name="show_sql">false</property> <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property> <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property> <property name="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu</property> </session-factory> </hibernate-configuration> 

In the test, I get the following exception stack trace:

 NHibernate.HibernateException was unhandled by user code Message=Could not create the driver from NHibernate.Driver.OracleDataClientDriver. Source=NHibernate StackTrace: at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 settings) in d:\CSharp\NH\nhibernate\src\NHibernate\Connection\ConnectionProvider.cs:line 113 at NHibernate.Connection.ConnectionProvider.Configure(IDictionary`2 settings) in d:\CSharp\NH\nhibernate\src\NHibernate\Connection\ConnectionProvider.cs:line 64 at NHibernate.Connection.ConnectionProviderFactory.NewConnectionProvider(IDictionary`2 settings) in d:\CSharp\NH\nhibernate\src\NHibernate\Connection\ConnectionProviderFactory.cs:line 50 at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Action`1 scriptAction, Boolean export, Boolean justDrop) in d:\CSharp\NH\nhibernate\src\NHibernate\Tool\hbm2ddl\SchemaExport.cs:line 333 at NHibernate.Tool.hbm2ddl.SchemaExport.Execute(Boolean script, Boolean export, Boolean justDrop) in d:\CSharp\NH\nhibernate\src\NHibernate\Tool\hbm2ddl\SchemaExport.cs:line 290 at IBCService.Tests.GenerateSchema_Fixture.Can_generate_schema() in D:\APPS\VS2010\IBanking\CustomerService\IBCService.Tests\GenerateSchema_Fixture.cs:line 21 InnerException: System.Reflection.TargetInvocationException Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache) at System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipVisibilityChecks, Boolean skipCheckThis, Boolean fillCache) at System.Activator.CreateInstance(Type type, Boolean nonPublic) at System.Activator.CreateInstance(Type type) at NHibernate.Bytecode.ActivatorObjectsFactory.CreateInstance(Type type) in d:\CSharp\NH\nhibernate\src\NHibernate\Bytecode\ActivatorObjectsFactory.cs:line 9 at NHibernate.Connection.ConnectionProvider.ConfigureDriver(IDictionary`2 settings) in d:\CSharp\NH\nhibernate\src\NHibernate\Connection\ConnectionProvider.cs:line 107 InnerException: System.NullReferenceException Message=Object reference not set to an instance of an object. Source=NHibernate StackTrace: at NHibernate.Driver.OracleDataClientDriver..ctor() in d:\CSharp\NH\nhibernate\src\NHibernate\Driver\OracleDataClientDriver.cs:line 42 InnerException: 

If I change NHibernate.Driver.OracleDataClientDriver to NHibernate.Driver.OracleClientDriver (MS Provider for Oracle), the test will succeed. Can someone tell me what I'm doing wrong?

+6
nhibernate
source share
3 answers

Hi I think the error occurs because Nhibernate does not load the driver from the GAC with Assembly.LoadWithPartialName (), but with Assembly.Load (). Try to place Oracle.DataAccess.dll in the bin directory or use the qualifyAssembly section in your app.config or web.config. Example:

 <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <qualifyAssembly partialName="Oracle.DataAccess" fullName="Oracle.DataAccess, Version=2.102.2.20, Culture=neutral, PublicKeyToken=89b483f429c47342" /> </assemblyBinding> </runtime> 
+13
source share

@PerlDev: I think you have the “ODP.NET 32 bit binary file” installed and you are compiling your application with the “AnyCPU” platform. If so, try switching to x86 (this is what I did and worked).

If you want to compile as x64, I think you should set "binary bit 64 bit ODP.NET" (I haven't done it yet).

+1
source share

It worked for me.

Go Build / Configuration Manager in Visual Studio. Check out the platform column. I changed debugging from any processor to x64

0
source share

All Articles