C# NHibernate 和 Oracle 托管客户端

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32229132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 02:59:56  来源:igfitidea点击:

C# NHibernate & Oracle Managed Client

c#oraclevisual-studionhibernatemanaged

提问by Jan Sr?eň

I have a big project that conceals another 16 project (Tests, Webs & App like Core, Email etc:.). I use C# MVC4 for my main website project. If I use a non Managed Client need me to do a project folder /binrecorded library Oracle.DataAccess.dll and everything works fine ( I must set(change) in Web.config -> param:

我有一个大项目,它隐藏了另外 16 个项目(测试、Web 和应用程序,如 Core、电子邮件等:)。我的主要网站项目使用 C# MVC4。如果我使用非托管客户端需要我做一个项目文件夹/bin记录库 Oracle.DataAccess.dll 并且一切正常(我必须在 Web.config -> param 中设置(更改):

<connectionStrings>
   <add name="PC13" connectionString="User Id=TEST; Password=TEST; Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=DBTEST)(PORT=1521))(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME = DBTEST)));" />   
</connectionStrings>

And DB connection works fine without ORA exception TNS_NAME ). I must set Data source to full path with tnsnames.ora when I use only alias like TEST i get a message "Exception: ORA-12154: TNS:could not resolve the connect identifier specified", but if i set full tns code for alias -> all works great.

并且 DB 连接工作正常,没有 ORA 异常 TNS_NAME )。当我只使用像 TEST 这样的别名时,我必须使用 tnsnames.ora 将数据源设置为完整路径,我收到一条消息“异常:ORA-12154:TNS:无法解析指定的连接标识符”,但如果我为别名设置了完整的 tns 代码-> 一切都很好。

I use hibernate.cfg.xmlfile:

我使用hibernate.cfg.xml文件:

<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
   <session-factory>
      <property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
      <property name="show_sql">true</property>
      <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
      <property name="command_timeout">60</property>
      <property name="cache.provider_class">NHibernate.Caches.SysCache2.SysCacheProvider, NHibernate.Caches.SysCache2</property>
      <property name="cache.use_second_level_cache">true</property>
      <property name="cache.use_query_cache">true</property>
      <property name="cache.default_expiration">120</property>
   </session-factory>  

My Web.configfile (for managed client add oracle.manageddataaccess.client part):

我的Web.config文件(对于托管客户端添加 oracle.manageddataaccess.client 部分):

<connectionStrings>
   <add name="PC13" connectionString="User Id=TEST; Password=TEST; Data Source=DBTEST;" />   
</connectionStrings>

<oracle.manageddataaccess.client>
    <version number="*">
      <dataSources>
        <dataSource alias="DBTEST" descriptor="(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=DBTEST)(PORT=1521))(CONNECT_DATA=(SERVER = DEDICATED)(SERVICE_NAME = DBTEST)))"/>
      </dataSources>
    </version>
</oracle.manageddataaccess.client>

I have dedicated DB server & I programming on Visual Studio 2010 SP1 32bit (Win 7 64b). I have installed Oracle client 11g (32 & 64b version). File hibernate.cfg.xml is is used in another 3 project in package like "IntegrationTest", "Core" etc:. I run program (F5) with Debug mode and x86 platform.

我有专用的数据库服务器,我在 Visual Studio 2010 SP1 32 位(Win 7 64b)上编程。我已经安装了 Oracle 客户端 11g(32 和 64b 版本)。文件 hibernate.cfg.xml 用于“IntegrationTest”、“Core”等包中的另外 3 个项目:我使用调试模式和 x86 平台运行程序 (F5)。

In project I use:

在项目中我使用:

  • NHibernate v. 3.3.1.4000
  • StructureMap v. 2.6.4.
  • and other unimportant Packages
  • NHibernate v. 3.3.1.4000
  • StructureMap v. 2.6.4。
  • 和其他不重要的包


What do I have to install the package and how to set it up? I Try add reference with Nuget

我必须安装什么包以及如何设置它?我尝试使用 Nuget 添加引用

  • Official Oracle ODP.NET, Managed Driver 12.1.22
  • Oracle Data Provider for .NET (ODP... 121.1.2
  • 官方 Oracle ODP.NET,托管驱动程序 12.1.22
  • 适用于 .NET 的 Oracle 数据提供程序 (ODP... 121.1.2

When I install some of these packages (add references from Nuget) to my Website project and i change hibernate.cfg.xml to:

当我将其中一些包(从 Nuget 添加引用)安装到我的网站项目时,我将 hibernate.cfg.xml 更改为:

<property name="connection.driver_class">NHibernate.Driver.OracleManagedDriver</property>

I got message from VS Exception "Could not create the driver from NHibernate.Driver.OracleManagedDriver"

我收到来自 VS 异常的消息“无法从 NHibernate.Driver.OracleManagedDriver 创建驱动程序”



If i go to the View -> Server Exploler-> to the Data Cennectionand i set a Add Connection . . .(see link -> An Easy Drive to .NET Manual) . I dont get a ODP.NET Managed option, only .NET Framework data provider for Oracle option, and when I try connection i get this message "BadImageFormatException. This will occur when running in 64 bit mode with the 32 bit Oracle client components installed"

如果我转到View -> Server Exploler-> Data Cennection并设置Add Connection 。. . (请参阅链接 -> .NET 手册的简易驱动器)。我没有得到 ODP.NET 托管选项,只有 Oracle 选项的 .NET Framework 数据提供程序,当我尝试连接时,我收到此消息“BadImageFormatException。在安装了 32 位 Oracle 客户端组件的 64 位模式下运行时会发生这种情况”

My question is

我的问题是

  1. What can I use for reference (driver) ?
  2. How can I set it ?
  1. 我可以使用什么作为参考(驱动程序)?
  2. 我该如何设置?

An Easy Drive to .NET Manual

.NET 的简单驱动器手册

Thanks a lot

非常感谢

回答by rshimoda

I am using NHibernate 4.0.4 and I have installed the "Oracle.ManagedDataAccess" nuget package (https://www.nuget.org/packages/Oracle.ManagedDataAccess/).

我正在使用 NHibernate 4.0.4 并且我已经安装了“Oracle.ManagedDataAccess”nuget 包(https://www.nuget.org/packages/Oracle.ManagedDataAccess/)。

In order to configure NHibernate to use the Oracle Managed Driver it is necessary to change just a tad bit the hibernate.cfg.xml file - and use the NHibernate.Driver.OracleManagedDataClientDriver as the "connection.driver_class".

为了将 NHibernate 配置为使用 Oracle 托管驱动程序,需要稍微更改 hibernate.cfg.xml 文件 - 并使用 NHibernate.Driver.OracleManagedDataClientDriver 作为“connection.driver_class”。

Therefore, my xml config file is as it follows:

因此,我的 xml 配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory>
    <property name="connection.driver_class">NHibernate.Driver.OracleManagedDataClientDriver</property>
    <property name="connection.connection_string">User Id=user;Password=pws;Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=10.10.10.18)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=SRV)))</property>
    <property name="show_sql">true</property>
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    <property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
  </session-factory>
</hibernate-configuration>

Good luck - I know that using Oracle and ORM can be quite an annoying experience, but one that is worth the effort in the end.

祝你好运 - 我知道使用 Oracle 和 ORM 可能是一种令人讨厌的体验,但最终还是值得付出努力的。