使用ODP.NET配置NHibernate 3.0

时间:2010-12-30 10:55:12

标签: nhibernate

我刚刚第一次设置了NHibernate。我的平台和配置设置如下:

  • 数据库:Oracle 11.1g
  • ODP.NET版本:4.112.1.2(从ODTWithODAC112012安装,这是我的Oracle数据库安装之上的1版本)
  • NHibernate 3.0版

我用测试项目创建了一个测试MVC应用程序。然后,为了测试NHibernate连接,我使用以下测试夹具:

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配置文件:

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

在测试中,我得到以下异常堆栈跟踪:

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: 

如果我将NHibernate.Driver.OracleDataClientDriver更改为NHibernate.Driver.OracleClientDriver(Oracle的MS提供程序),则测试成功。有人能告诉我我做错了吗?

3 个答案:

答案 0 :(得分:13)

您好 我认为错误发生了,因为Nhibernate没有使用Assembly.LoadWithPartialName()从GAC加载驱动程序,而是使用Assembly.Load()。尝试将Oracle.DataAccess.dll放在bin目录中,或将qualifyAssembly部分放入app.config或web.config中。 例如:

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

答案 1 :(得分:1)

@PerlDev:我认为您安装了“ODP.NET 32位二进制文​​件”,并且您正在使用“AnyCPU”平台编译您的应用程序。如果是这样,请尝试更改为x86(这就是我所做的,并且有效)。

如果你想编译为x64,我认为你必须安装“ODP.NET 64位二进制文​​件”(我还没有这样做)。

答案 2 :(得分:0)

这对我有用。

在Visual Studio中使用Build / Configuration Manager。检查平台列。我将调试从任何CPU更改为x64