zoukankan      html  css  js  c++  java
  • Castle ActiveRecord学习实践(10)——使用多数据库

    我们在开发中会遇到这样的情况,创建的ActiveRecord类在不同的数据库中,本文来看看Castle ActiveRecord如何处理这种情况。

    比如我们有 DB1数据库,里面包含一个Articles表,DB2数据库,里面包含一个AppInfo表

    首先,我们创建抽象基类

       1:  public abstract class DB1<T> : ActiveRecordBase<T>
       2:  {
       3:  }
       1:  public abstract class DB2<T> : ActiveRecordBase<T>
       2:  {
       3:  }

    抽象基类里面什么都没有,只是个空架子

    其次,创建具体类

       1:  [ActiveRecord("Articles")]
       2:  public class Article : DB1<Article>
       3:  {
       4:      [PrimaryKey("id")]
       5:      public int Id
       6:      {
       7:          get;
       8:          set;
       9:      }
      10:      [Property("title")]
      11:      public string Title
      12:      {
      13:          get;
      14:          set;
      15:      }
      16:      [Property("text")]
      17:      public string Text
      18:      {
      19:          set;
      20:          get;
      21:      }
      22:  }
       1:  [ActiveRecord]
       2:  public class AppInfo : DB2<AppInfo>
       3:  {
       4:      [PrimaryKey("id")]
       5:      public int Id
       6:      {
       7:          get;
       8:          set;
       9:      }
      10:      [Property("appname")]
      11:      public string AppName
      12:      {
      13:          get;
      14:          set;
      15:      }
      16:      [Property("text")]
      17:      public string Text
      18:      {
      19:          get;
      20:          set;
      21:      }
      22:  }

    最后,配置文件

       1:  <activeRecord isWeb="true">
       2:    <config type="Model.DB1`1,Model">
       3:      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
       7:      <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
       4:      <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
       5:      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
       6:      <add key="connection.connection_string" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB1.mdf;Integrated Security=True;User Instance=True"/>
       8:    </config>
       9:    <config type="Model.DB2`1,Model">
      10:      <add key="hibernate.connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
      11:      <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect"/>
      12:      <add key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
      13:      <add key="connection.connection_string" value="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB2.mdf;Integrated Security=True;User Instance=True"/>
      14:      <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
      15:    </config>
      16:  </activeRecord>

    global类

       1:  protected void Application_Start(object sender, EventArgs e)
       2:  {
       3:      IConfigurationSource source = ConfigurationManager.GetSection("activeRecord") as IConfigurationSource;
       4:   
       5:      Castle.ActiveRecord.ActiveRecordStarter.Initialize(System.Reflection.Assembly.Load("Model"), source);
       6:   
       7:  }

    这样就完成了

    需要注意:DB1和DB2是泛型类,它指定的类型名称为  type="Model.DB1`1,Model"

     

  • 相关阅读:
    【解决方案】流媒体协议RTSP视频平台EasyNVR远程监控系统在水利工程场景中的应用
    网络流媒体协议RTSP视频平台EasyNVR程序启动失败,端口被占用如何解决?
    视频监控联网RTSP平台EasyNVR用户管理权限与实际权限不匹配,该如何排查?
    流媒体协议RTSP视频平台EasyNVR录像回看列表删除提示日期错误,遇此问题该怎么解决?
    拉流失败,高清视频监控平台EasyNVR的RTSP摄像头被禁用怎么办?
    流媒体协议RTSP视频平台EasyNVR在windows下编译报错,该如何解决?
    如何解决RTSP协议视频平台EasyNVR智能云终端登录界面一直循环导致无法登录?
    RTSP视频平台EasyNVR如何设置防火墙允许程序运行端口的访问?
    【搭建方案】互联网电视如何通过EasyNVR视频平台传输直播视频流?
    RTSP视频平台EasyNVR录像版重启软件后服务崩溃问题排查解决
  • 原文地址:https://www.cnblogs.com/whx1973/p/2844992.html
Copyright © 2011-2022 走看看