NHibernate连接Oracle配置问题
时间:2010-09-05 来源:kasoo
这几天在做NHibernate连接Oracle的测试,配置文件是从NHibernate发行包里拷贝出来的,做了简单的连接字符的修改,配置文件如下:
代码<?xml version="1.0" encoding="utf-8"?>
<!--
This template was written to work with NHibernate.Test.
Copy the template to your NHibernate.Test project folder and rename it in hibernate.cfg.xml and change it
for your own use before compile tests in VisualStudio.
-->
<!-- This is the System.Data.OracleClient.dll provider for Oracle from MS -->
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory name="NHibernate.Test">
<property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property>
<property name="connection.connection_string">
User ID=xx;Password=xx;Data Source=xx;
</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>
<mapping assembly="Model"/>
</session-factory>
</hibernate-configuration>
修改了两处内容:
一修改dialect为NHibernate.Dialect.Oracle10gDialect,(我用的是oracle10g)原来的为NHibernate.Dialect.OracleDialect,不修改会提示映射文件不能编译,这个错误有点奇怪。
二增加mapping节点,来指定映射文件所在程序集
好了,简单的写个测试程序,再测试一下,OK测试能过。
我们看到在配置文件中有一行:<!-- This is the System.Data.OracleClient.dll provider for Oracle from MS -->,这里使用的是微软提供的驱动程序,我们改成Oracle提供的试试。在配置文件中把NHibernate.Driver.OracleClientDrivern改成NHibernate.Driver.OracleDataClientDrivern。OK再测试,提示“Could not create the driver from NHibernate.Driver.OracleDataClientDriver.”,应该是我们没有添加Oracle.DataAccess的引用,我们来添加引用,很奇怪在添加程序集的窗口中的.Net选项卡,找不到,没有关系,我们在浏览选项卡里找,在“X:\oracle\product\10.2.0\client_1\BIN”目录下找到并选择Oracle.DataAccess.dll,再来做测试。还是提示“Could not create the driver from NHibernate.Driver.OracleDataClientDriver.”,我们找到Oracle.DataAccess.dll,拷贝到测试程序的bin目录下,再运行。这次还是报错,不过错误提示变了,“Unable to cast object of type 'Oracle.DataAccess.Client.OracleConnection' to type 'System.Data.Common.DbConnection'.”,上网google了好久,没有找到解决办法,后来看到园友1-2-3在用NHibernate调用Oracle的存储过程 这篇文章找到了解决办法,在配置文件中增加一行:“<property name="hbm2ddl.keywords">none</property>”,再次执行我们的测试程序,这次执行通过。
微软和Oracle都提供了.net连接Oracle数据库的驱动程序,一般认为Oracle所提供的驱动程序性能上要优于微软提供的。在.net4.0中使用System.Data.OracleClient时,会得到警告信息:“'System.Data.OracleClient.OracleConnection' is obsolete: 'OracleConnection has been deprecated. http://go.microsoft.com/fwlink/?LinkID=144260' ”,在给出的这个链接上有这么一句话:“The types in System.Data.OracleClient are deprecated. The types are supported in version 4 of the .NET Framework but will be removed in a future release. Microsoft recommends that you use a third-party Oracle provider.”意思是System.Data.OracleClient是不再被推荐使用的,并且在4.0以后的版本将被移除,微软推荐你使用Oracle提供的驱动程序。