nhibernate3.0连接oracle11g( 版本11.1.0.6.0)

Coordinator
Aug 22, 2011 at 4:00 AM

通过ODAC方式连接.

1.下载支持oracle11g的ODAC

Each 32-bit ODAC client download below supports connectivity to Oracle Database versions 9.2, 10.1, 10.2, and 11.1, and 11.2.

各版本详情下载

http://www.oracle.com/technetwork/database/windows/downloads/index-101290.html

明细情况

http://www.oracle.com/technetwork/database/windows/downloads/utilsoft-087491.html

2.我下载了最全版的"ORAC1110621.zip"安装后,生成

$ORACLE_HOME$\11.1.0\db_1\ODP.NET\bin.默认有支持.net1和.net2的框架如下图

以.net2.0为例

其中oraprovcfg.exe可以注册相关信息,暂时不用,通过手动方式加入,同于文档中暂未提到支持.net4.0,所以我只针对一个项目增加该配置信息

 

3.安装后会自动注册machine.config中

%SystemRoot%\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config

在结点中分别增加了这两处代码与oracle.dataaccess相关,后面有用.

========================

  <configSections>
    <section name="oracle.dataaccess.client" type="System.Data.Common.DbProviderConfigurationHandler, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
</configSections>

=========================

 <system.data>
    <DbProviderFactories>
      <add name="Oracle Data Provider for .NET" invariant="Oracle.DataAccess.Client" description="Oracle Data Provider for .NET" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=2.111.6.20, Culture=neutral, PublicKeyToken=89b483f429c47342"/>

       </DbProviderFactories>
  </system.data>

========================
4.配置nhibernate配置文件中的连接方式等信息.

   <db:provider id="DbProvider"  provider="OracleODP-2.0"
                     connectionString="Data Source=tns;User Id=username;Password=password;"/>

=================================================

  <object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate30">
    <property name="DbProvider" ref="DbProvider"/>

    <property name="MappingAssemblies">
      <list>
        <value>WebApp.Model</value>
      </list>
    </property>
     <property name="HibernateProperties">
      <dictionary>
        <entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
          <entry key="dialect" value="NHibernate.Dialect.Oracle10gDialect"/>
          <entry key="connection.driver_class" value="NHibernate.Driver.OracleDataClientDriver"/><!--通过这个方式会自动调用oracle.dataaccess,用反编译工具可以看到代码中写死了这个连接-->
          <entry key="hbm2ddl.keywords" value="none"/>

  <entry key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
      </dictionary>
    </property>

  </object>

 =============================================================

5.由于安装时,默认不会修改.net4.0框架中这部份的machine.config信息,所以参考.net2.0中自动生成的machine.config,加入oracle.dataaccess到项目配置文件web.config中

以下两步骤抄第3步中的信息.

======================================
    <configSections>

<section name="oracle.dataaccess.client" type="System.Data.Common.DbProviderConfigurationHandler, System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
    </configSections>

==================================================

    <system.data>
        <DbProviderFactories>
            <add name="Oracle Data Provider for .NET" invariant="Oracle.DataAccess.Client" description="Oracle Data Provider for .NET" type="Oracle.DataAccess.Client.OracleClientFactory, Oracle.DataAccess, Version=2.111.6.20, Culture=neutral, PublicKeyToken=89b483f429c47342"/>
        </DbProviderFactories>
    </system.data>   

 

]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]  nhibernate连接mysql]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]

 附:注连接mysql

http://dev.mysql.com/downloads/connector/net/6.4.html

下载mysql-connector-net-6.4.3.msi

安装后

.net4.0框架的machine.config

会自动增加

====================================

    <DbProviderFactories>
      <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
    </DbProviderFactories>

===================================

  <connectionStrings>
       <add name="LocalMySqlServer" connectionString="" />
  </connectionStrings>

======================================

  <db:provider id="DbProvider"  provider="MySql-5.0"
                   connectionString="Data Source=localhost;User Id=username;Password=pwd;database=dbname"/>

=============================

<entry key="dialect" value="NHibernate.Dialect.MySQLDialect"/>
          <entry key="hibernate.connection.driver_class" value="NHibernate.Driver.MySqlDataDriver"/>

 

附注:相关异常小结

1 找不到请求的 .Net Framework Data Provider。可能没有安装。

原因:

   虽然按照上面步骤安装odp,但默认是不会加入.net4.0中的machine.config.

解决:

  方案一:采用上述方式在独立的项目web.config中加入相关结点信息.

  方案二:采用ODP.NET下提供的oraprovcfg来生成配置信息,直接加入相应.net4.0框架machine.config中.命令如下:

oraprovcfg /action:config /product:odp /frameworkversion:v4.0.30319 /providerpath:D:\Oracle\app\xxx\product\11.1.0\db_1\ODP.NET\bin\2.x\Oracle.DataAccess.dll

 其中,providerpath带的参数必须是全路径信息.

2. NHibernate.Driver.OracleDataClientDriver...ctor()这个代表构造函数异常

TargetInvocationException: 调用的目标发生了异常。

原因:

在生成的bin文件中没有包括引入的Oracle.DataAccess.dll这个动态库,其他的在编辑时都会自动生成,不知道为什么这个没有自动生成.

解决方案:

1.在web.config中加入

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

2.同时拷备Oracle.DataAccess.dll,到项目生成文件假bin中.

 

Coordinator
Nov 17, 2011 at 11:37 AM

mysql配置实例

<?xml version="1.0"?>
<configuration>
    <system.data>
        <DbProviderFactories>
            <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient" description=".Net Framework Data Provider for MySQL" type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
        </DbProviderFactories>
    </system.data>
   
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <qualifyAssembly partialName="MySql.Data" fullName="MySql.Data, Version=6.4.3.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d" />
        </assemblyBinding>
    </runtime>
</configuration>