我正在使用NHibernate创建我的第一个应用程序。不幸的是,我遇到了一个我无法解决的问题:
我正在从包含CRUD方法和NHibernateHelper类的“DAL”项目访问数据库。
我写了一个测试项目,我试图将一个对象添加到数据库 - 它完美地工作。但是当我尝试从我的应用程序的ViewModel中调用相同的方法时(它也在另一个项目中),它会在下面的代码中的 configuration.BuildSessionFactory()处抛出错误:
private static ISessionFactory SessionFactory
{
get
{
if (_sessionFactory == null)
{
var configuration = new Configuration();
configuration.Configure();
configuration.AddAssembly(typeof(Address).Assembly);
_sessionFactory = configuration.BuildSessionFactory();
}
return _sessionFactory;
}
}
例外:
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."}
在我的测试项目中,我添加了对Oracle.DataAccess.dll的引用并创建了App.config,因为它在没有它的情况下抛出相同的错误:
<?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>
我在包含ViewModel的项目中做了同样的事情,我需要从DB中获取数据,但它没有帮助。我也将Copy Local设置为True作为参考,但无济于事。
这是我的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>
我正在使用NHibernate v 3.3.1.4000,Oracle 11g xe和64位Windows 7(我不能选择在32位或64位上调试,只有一种可能选择:“Active(任何CPU)” )
我可以从测试项目中使用它的原因是什么,但我不能从另一个中做到这一点?
提前谢谢
答案 0 :(得分:1)
您是否在Web项目或DAL中引用了驱动程序的dll?它应该在您的Web项目上,Copy Local设置为true,请参阅此类question
答案 1 :(得分:1)
问题解决了。我不得不在主项目中放置app.config,而不是在我使用数据库的dll库中。我不知道放在类库中的app.config会被忽略。