nhibernate多数据库配置总结(C# )

 

最近做了个小工具,要使用sqlite和sqlserver,后台的表结构都一样。原来程序运行时无法做到同时使用2个库。后来找了些资料,整理了下。

hibernate.cfg.config内容如下,第一个是sqlite配置,第二个是sqlserver配置

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

 

 c#用字典管理sessionFactory,调用的时候使用GetSession(“sqlserver”)或者GetSession(“sqlite”)即可

   public static ISession GetSession(string strkey)
        {
            if (!mdic_sessionFactory.ContainsKey(strkey))
            {
                lock (m_lockHelper)
                {
                    if (!mdic_sessionFactory.ContainsKey(strkey))
                    {
                       
                        string path=@"D:\code\SummaryTool\CSPlugin\bin\Debug\hibernate.cfg.config";
                        //
                        Configuration m_configurationtmp = new Configuration();
                        XmlDocument xdoc = new XmlDocument();
                        xdoc.Load(path);
                  

                        XmlNode xn =null ;
                        foreach (XmlNode xnsub in xdoc.DocumentElement.ChildNodes)
                        {

                            if (xnsub.Attributes["name"].Value == strkey)
                            {

                                xn = xnsub;
                                break;
                            }
                       
                        }
                   
                        XmlTextReader xtr = new XmlTextReader(new StringReader(xn.OuterXml));
                        m_configurationtmp.Configure(xtr);
                        m_configurationtmp.AddAssembly("ServiceCoreModel");

                        mdic_sessionFactory[strkey] = m_configurationtmp.BuildSessionFactory();
                    }
                }
            }
            return mdic_sessionFactory[strkey].OpenSession();
        }

 

业务对象映射(使用OrmCodeGenerator2.0.exe,但是需要少量修改,不能直接用):

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" >
  <class name="ServiceCoreModel.RelicResearch.fileinfor, ServiceCoreModel" table="fileinfor" lazy="false">
    <id name="GUID" column="guid" type="String(36)">
      <generator class="assigned" />
    </id>
    <property name="FilePath" type="String(200)" column="fpath" />
    <property name="MyOp" type="String(10)" column="operation" />
    <property name="MyDate" type="String(20)" column="date" />
    <property name="FileSize" type="Double" column="fsize" />
    <property name="Suffix" type="String(20)" column="suffix" />
    <property name="Userid" type="String(36)" column="userid" />
  </class>
 
</hibernate-mapping>
---------------------------------------

using System;
using System.Collections;
using Iesi.Collections;
namespace ServiceCoreModel.RelicResearch
{
   /// <summary>
   ///功能描述    :   
   ///开发者      :   
   ///建立时间    :    2011/6/3 23:24:15
   ///修订描述    :   
   ///进度描述    :   
   ///版本号      :    1.0
   ///最后修改时间:    2011/6/3 23:24:15
   ///
   ///Function Description :   
   ///Developer                :   
   ///Builded Date:    2011/6/3 23:24:15
   ///Revision Description :   
   ///Progress Description :   
   ///Version Number        :    1.0
   ///Last Modify Date     :    2011/6/3 23:24:15
   /// </summary>
   public class fileinfor
   {
      #region 构造函数
       public fileinfor()
      {}

      //public diskinfor(int id, float amount)
      //{
      //   this.diskid=id;
      //   this.diskamount=amount;
      //}
      #endregion

      #region 成员
      private string guid;
      private string userid;
      private string myop;
      private string mydate;
      private string filepath;
      private double filesize;
      private string suffix;
      #endregion


      #region 属性
      public virtual string GUID
      {
          get { return guid; }
          set { guid = value; }
      }
      public virtual string Userid
      {
          get { return userid; }
          set { userid = value; }
      }
      public virtual string MyOp
      {
          get { return myop; }
          set { myop = value; }
      }
      public virtual string MyDate
      {
          get { return mydate; }
          set { mydate = value; }
      }
      public virtual string FilePath
      {
          get { return filepath; }
          set { filepath = value; }
      }

      public virtual double  FileSize
      {
          get { return filesize; }
          set { filesize = value; }
      }
      public virtual string Suffix
      {
          get { return suffix; }
          set { suffix = value; }
      }
      #endregion

   }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值