I’m creating my first application using NHibernate. Unfortunately, I encountered a problem I cannot solve:
I’m accessing the database from “DAL” project that contains CRUD methods and NHibernateHelper class.
I wrote a test project and I tried to add an object to DB – it works perfectly. But when I try to call the same method from my application’s ViewModel (it’s in the other project as well) it throws an error at configuration.BuildSessionFactory() in the code below:
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(Address).Assembly);
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
The exception:
NHibernate.HibernateException: Could not create the driver from NHibernate.Driver.OracleDataClientDriver.
System.Reflection.TargetInvocationException: {"Exception has been thrown by the target of an invocation."}
System.NullReferenceException: {"Object reference not set to an instance of an object."}
In my test project I added reference to Oracle.DataAccess.dll and created App.config as it throws the same error without it:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<qualifyAssembly partialName="Oracle.DataAccess"
fullName="Oracle.DataAccess,
Version=4.112.2.0,
Culture=neutral,
PublicKeyToken=89b483f429c47342" />
</assemblyBinding>
</runtime>
</configuration>
I made the same thing in my project containing ViewModel in which I need to get data from DB, but it didn’t help. I also set Copy Local to True for that reference, but to no avail.
It’s my App.config:
<?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.Oracle10gDialect
</property>
<property name="connection.driver_class">
NHibernate.Driver.OracleDataClientDriver
</property>
<property name="connection.connection_string">
User Id=****;
Password=****;
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=
(PROTOCOL=TCP)(HOST=localhost)(PORT=1521)))
(CONNECT_DATA=(SERVICE_NAME=xe)));
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>
</session-factory>
</hibernate-configuration>
I’m using NHibernate v 3.3.1.4000, Oracle 11g xe and 64-bit Windows 7 (I cannot choose to debug on 32 or 64-bit, there is only one possibility to select: “Active (Any CPU)” )
What can be the reason that I can use it from test project, but I cannot do it from the other?
Thank you in advance
The problem is solved. I had to put app.config in main project, not in dll library from which I use the database. I didn’t know that app.config placed in class library will be ignored.