NHibernate4使用Oracle.ManagedDataAccess.dll连接oracle及配置多个数据库连接

Posted 雄关漫道真如铁,而今迈步从头越。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了NHibernate4使用Oracle.ManagedDataAccess.dll连接oracle及配置多个数据库连接相关的知识,希望对你有一定的参考价值。

NHibernate数据库配置参数在hibernate.cfg.xml中

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">
  <session-factory name="ora10gFactory">
    <!--<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>-->
    <!--property name="connection.driver_class">NHibernate.Driver.OracleClientDriver</property-->
    <property name="dialect">NHibernate.Dialect.Oracle10gDialect</property>
    <property name="connection.driver_class">NHibernate.Driver.OracleManagedDataClientDriver</property>
    <property name="connection.connection_string">
      User ID=jg122;Password=jg122;Data Source=127.0.0.1:1521/orcl
    </property>
    <property name="show_sql">true</property>
    <property name="query.substitutions">true 1, false 0, yes \'Y\', no \'N\'</property>
    <!--<property name="current_session_context_class">managed_web</property>
    <property name="proxyfactory.factory_class">NHibernate.Bytecode.DefaultProxyFactoryFactory,NHibernate</property>-->
    <property name="hbm2ddl.keywords">none</property>
    <!--加载映射-->
    <mapping assembly="Spring.NHibernate.Model"/>
  </session-factory>
</hibernate-configuration>

其中dialect和driver值,可以从对象浏览器中查看NHibernate.dll的结构进行选择。

    

oracle连接使用的是Oracle.ManagedDataAccess.dll,因此driver选择OracleManagedDataClientDriver,Dialect选择最新的Oracle10gDialect

创建数据库实例类代码:

public class NHibernateHelper
    {
        private static ISessionFactory _sessionFactory;

        /// <summary>
        /// 创建ISessionFactory
        /// </summary>
        public static ISessionFactory SessionFactory
        {
            get
            {
                // 默认从 App.config,web.config或者hibernate.cfg.xml查找配置文件;
                //var cfg = new Configuration().Configure("");
                //// 如果配置文件不是以上“App.config,web.config或者hibernate.cfg.xml”,是自定义的配置文件名称;
                //// 要注意使用MSTest进行单元测试时,请写配置文件的绝对路径,否则找不到;
                ////var cfg = new NHibernate.Cfg.Configuration().Configure("D:/ProjectStudyTest/NHibernate/NHibernateStudy/Lesson3.Dao/Config/hibernate.cfg.xml");

                ////如果用户Nunit进行单元测试,写相对路径即可。
                ////var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");

                //sessionFactory = cfg.BuildSessionFactory();


                //配置ISessionFactory
                var cfg = (new Configuration()).Configure();
                return _sessionFactory == null ? cfg.BuildSessionFactory() : _sessionFactory;
            }
        }
    }

 

系统默认会在部署根目录下从App.config,web.config或者hibernate.cfg.xml查找配置参数;

       如果要指定nhibernate数据库配置文件,可以使用Configure的重载方法,传入路径参数。

方法一:创建多个cfg.xml配置文件,实现连接多个数据库

 1 public class NHibernateHelper
 2     {
 3         private static ISessionFactory _sessionFactory;
 4 
 5         /// <summary>
 6         /// 创建ISessionFactory
 7         /// </summary>
 8         public static ISessionFactory SessionFactory
 9         {
10             get
11             {
12                 // 默认从 App.config,web.config或者hibernate.cfg.xml查找配置文件;
13                 //var cfg = new Configuration().Configure("");
14                 //// 如果配置文件不是以上“App.config,web.config或者hibernate.cfg.xml”,是自定义的配置文件名称;
15                 //// 要注意使用MSTest进行单元测试时,请写配置文件的绝对路径,否则找不到;
16                 ////var cfg = new NHibernate.Cfg.Configuration().Configure("D:/ProjectStudyTest/NHibernate/NHibernateStudy/Lesson3.Dao/Config/hibernate.cfg.xml");
17 
18                 ////如果用户Nunit进行单元测试,写相对路径即可。
19                 ////var cfg = new NHibernate.Cfg.Configuration().Configure("Config/hibernate.cfg.xml");
20 
21                 //sessionFactory = cfg.BuildSessionFactory();
22 
23                 string path = AppDomain.CurrentDomain.BaseDirectory + "hibernate2.cfg.xml";
24                 //配置ISessionFactory
25                 var cfg = (new Configuration()).Configure(path);
26                 return _sessionFactory == null ? cfg.BuildSessionFactory() : _sessionFactory;
27             }
28         }
29     }

 

方法二:在hibernate.cfg.xml中配置多个数据库连接参数

<?xml version="1.0" encoding="utf-8"?>
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2">

  <session-factory name="sqlite">
    <property name="connection.driver_class">NHibernate.Driver.SQLite20Driver</property>
    <property name="connection.connection_string">
      Data Source=D:\\code\\SummaryTool\\CSPlugin\\bin\\Debug\\test.db;Version=3;New=False;
    </property>
    <property name="dialect">NHibernate.Dialect.SQLiteDialect</property>
    <property name="query.substitutions">true=1;false=0</property>
    <property name="show_sql">true</property>
    <property name="use_outer_join">true</property>
  </session-factory>

  <session-factory name="sqlserver">
   <property name="connection.provider">NHibernate.Connection.DriverConnectionProvider, NHibernate</property>
      <property name="prepare_sql">false</property>
  
   <property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
   <property name="connection.connection_string">
        Server=(local);initial catalog=***;Integrated Security=SSPI;User ID=***;Password=***
      </property>
   <property name="show_sql">true</property>
    
   <property name="dialect">NHibernate.Dialect.MsSql2000Dialect</property>
  </session-factory>

</hibernate-configuration>

创建数据库实例代码:

 1 public static ISession GetSession(string strkey)
 2 {
 3     if (!mdic_sessionFactory.ContainsKey(strkey))
 4     {
 5         lock (m_lockHelper)
 6         {
 7             if (!mdic_sessionFactory.ContainsKey(strkey))
 8             {
 9 
10                 string path = AppDomain.CurrentDomain.BaseDirectory + "hibernate.cfg.xml";
11                 //
12                 Configuration m_configurationtmp = new Configuration();
13                 XmlDocument xdoc = new XmlDocument();
14                 xdoc.Load(path);
15 
16 
17                 XmlNode xn = null;
18                 foreach (XmlNode xnsub in xdoc.DocumentElement.ChildNodes)
19                 {
20 
21                     if (xnsub.Attributes["name"].Value == strkey)
22                     {
23 
24                         xn = xnsub;
25                         break;
26                     }
27 
28                 }
29 
30                 XmlTextReader xtr = new XmlTextReader(new StringReader(xn.OuterXml));
31                 m_configurationtmp.Configure(xtr);
32                 //m_configurationtmp.AddAssembly("ServiceCoreModel");
33 
34                 mdic_sessionFactory[strkey] = m_configurationtmp.BuildSessionFactory();
35             }
36         }
37     }
38     return mdic_sessionFactory[strkey].OpenSession();
39 }

 

以上是关于NHibernate4使用Oracle.ManagedDataAccess.dll连接oracle及配置多个数据库连接的主要内容,如果未能解决你的问题,请参考以下文章

NHibernate:如何添加多个程序集?

Nhibernate一对多关系复合键问题

IList<int> 的流畅 NHibernate 映射?

nHibernate 4.1.4没有引用与保留关键字匹配的表名

NHibernate在脚下射击:协会所有权和版本控制

NHibernate HQL 生成器支持 SQL Server 2016 时态表