你的位置:首页 > ASP.net教程

[ASP.net教程]Castle.ActiveRecord多数据库配置


最近使用Castle.ActiveRecord框架,网上关于多数据支持的文章很少,因此有了这篇博文的产生。

  1. 开发工具VS2015,Sql Server2008R2

  2. 新建数据库,数据初始化脚本如下:

 1 --新建数据库Castle.ActiveRecord.DB1 2 CREATE DATABASE [Castle.ActiveRecord.DB1]; 3 GO 4  5 USE [Castle.ActiveRecord.DB1] 6 GO 7 /****** Object: Table [dbo].[Post]  Script Date: 09/25/2016 16:46:21 ******/ 8 SET ANSI_NULLS ON 9 GO10 SET QUOTED_IDENTIFIER ON11 GO12 CREATE TABLE [dbo].[Post](13   [Id] [int] IDENTITY(1,1) NOT NULL,14   [Subject] [nvarchar](64) NULL,15   [Text] [nvarchar](1024) NULL,16   [DateAdded] [datetime] NULL,17 PRIMARY KEY CLUSTERED 18 (19   [Id] ASC20 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]21 ) ON [PRIMARY]22 GO23 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'自增主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Post', @level2type=N'COLUMN',@level2name=N'Id'24 GO25 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主题' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Post', @level2type=N'COLUMN',@level2name=N'Subject'26 GO27 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'内容' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Post', @level2type=N'COLUMN',@level2name=N'Text'28 GO29 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'添加日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Post', @level2type=N'COLUMN',@level2name=N'DateAdded'30 GO31 SET IDENTITY_INSERT [dbo].[Post] ON32 INSERT [dbo].[Post] ([Id], [Subject], [Text], [DateAdded]) VALUES (1, N'新闻', N'最新新闻内容', '2016-09-01')33 INSERT [dbo].[Post] ([Id], [Subject], [Text], [DateAdded]) VALUES (2, N'音乐', N'流行音乐', '2016-09-02')34 SET IDENTITY_INSERT [dbo].[Post] OFF35 36 --新建数据库Castle.ActiveRecord.DB237 CREATE DATABASE [Castle.ActiveRecord.DB2];38 GO39 40 USE [Castle.ActiveRecord.DB2]41 GO42 /****** Object: Table [dbo].[Post]  Script Date: 09/25/2016 16:53:05 ******/43 SET ANSI_NULLS ON44 GO45 SET QUOTED_IDENTIFIER ON46 GO47 CREATE TABLE [dbo].[Post](48   [Id] [int] IDENTITY(1,1) NOT NULL,49   [Subject] [nvarchar](64) NULL,50   [Text] [nvarchar](1024) NULL,51   [DateAdded] [datetime] NULL,52 PRIMARY KEY CLUSTERED 53 (54   [Id] ASC55 )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]56 ) ON [PRIMARY]57 GO58 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'自增主键' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Post', @level2type=N'COLUMN',@level2name=N'Id'59 GO60 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'主题' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Post', @level2type=N'COLUMN',@level2name=N'Subject'61 GO62 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'内容' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Post', @level2type=N'COLUMN',@level2name=N'Text'63 GO64 EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'添加日期' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'Post', @level2type=N'COLUMN',@level2name=N'DateAdded'65 GO66 SET IDENTITY_INSERT [dbo].[Post] ON67 INSERT [dbo].[Post] ([Id], [Subject], [Text], [DateAdded]) VALUES (1, N'小说', N'纪实小说', '2016-09-03')68 INSERT [dbo].[Post] ([Id], [Subject], [Text], [DateAdded]) VALUES (2, N'电脑', N'超极本', '2016-09-04')69 SET IDENTITY_INSERT [dbo].[Post] OFF

    3.新建MVC项目ActiveRecord.Demo,新建类库Castle.ActiveRecord.DB1.Models,类库Castle.ActiveRecord.DB2.Models

  • MVC项目ActiveRecord.Demo文件添加Castle.ActiveRecord框架dll引用,使用NuGut搜索添加.添加对类库Castle.ActiveRecord.DB1.Models,类库Castle.ActiveRecord.DB2.Models的引用。
  • 类库Castle.ActiveRecord.DB1.Models,类库Castle.ActiveRecord.DB2.Models添加Castle.ActiveRecord、NHibernate引用。

 4.Castle.ActiveRecord.DB1.Models类库添加DB1ActiveRecordBase抽象类文件,继承自ActiveRecordBase基类.

  添加Post类,继承自DB1ActiveRecordBase抽象类

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Castle.ActiveRecord.DB1.Models 8 { 9   public abstract class DB1ActiveRecordBase : ActiveRecordBase10   {11 12   }13 }

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Castle.ActiveRecord.DB1.Models 8 { 9   [ActiveRecord("Post")]10   public class Post : DB1ActiveRecordBase11   {12     /// <summary>13     /// 自增主键14     /// </summary>15     [PrimaryKey(PrimaryKeyType.Identity, Column = "Id")]16     public int Id { set; get; }17 18 19     /// <summary>20     /// 主题21     /// </summary>22     [Property("Subject")]23     public string Subject { set; get; }24 25 26     /// <summary>27     /// 内容28     /// </summary>29     [Property("Text")]30     public string Text { set; get; }31 32     /// <summary>33     /// 添加日期34     /// </summary>35     [Property("DateAdded")]36     public DateTime DateAdded { set; get; }37 38     public static IEnumerable<Post> FindAllBy()39     {40       return FindAll(typeof(Post)).Cast<Post>(); ;41     }42   }43 }


Castle.ActiveRecord.DB2.Models类库添加DB2ActiveRecordBase抽象类文件,继承自ActiveRecordBase基类
添加Post类,继承自DB2ActiveRecordBase抽象类

 

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Castle.ActiveRecord.DB2.Models 8 { 9   public abstract class DB2ActiveRecordBase : ActiveRecordBase10   {11 12   }13 }

 1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6  7 namespace Castle.ActiveRecord.DB2.Models 8 { 9   [ActiveRecord("Post")]10   public class Post : DB2ActiveRecordBase11   {12     /// <summary>13     /// 自增主键14     /// </summary>15     [PrimaryKey(PrimaryKeyType.Identity, Column = "Id")]16     public int Id { set; get; }17 18 19     /// <summary>20     /// 主题21     /// </summary>22     [Property("Subject")]23     public string Subject { set; get; }24 25 26     /// <summary>27     /// 内容28     /// </summary>29     [Property("Text")]30     public string Text { set; get; }31 32     /// <summary>33     /// 添加日期34     /// </summary>35     [Property("DateAdded")]36     public DateTime DateAdded { set; get; }37 38     public static IEnumerable<Post> FindAllBy()39     {40       return FindAll(typeof(Post)).Cast<Post>(); ;41     }42   }43 }

5.MVC项目ActiveRecord.Demo项目Web.config配置多数据库,主要是config节的type的配置

<configSections>  <section name="activerecord"       type="Castle.ActiveRecord.Framework.Config.ActiveRecordSectionHandler, Castle.ActiveRecord" /> </configSections> <connectionStrings>  <add name="Castle.ActiveRecord.DB1" connectionString="Data Source=.;Initial Catalog=Castle.ActiveRecord.DB1;Integrated Security=SSPI"/>  <add name="Castle.ActiveRecord.DB2" connectionString="Data Source=.;Initial Catalog=Castle.ActiveRecord.DB2;Integrated Security=SSPI"/> </connectionStrings> <activerecord isWeb="true">  <config type="Castle.ActiveRecord.DB1.Models.DB1ActiveRecordBase,Castle.ActiveRecord.DB1.Models">   <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />   <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" />   <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />   <add key="connection.connection_string_name" value="Castle.ActiveRecord.DB1" />   <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />  </config>  <config type="Castle.ActiveRecord.DB2.Models.DB2ActiveRecordBase,Castle.ActiveRecord.DB2.Models">   <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver" />   <add key="dialect" value="NHibernate.Dialect.MsSql2005Dialect" />   <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />   <add key="connection.connection_string_name" value="Castle.ActiveRecord.DB2" />   <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle" />  </config> </activerecord>

6.ActiveRecord.Demo项目Global文件Application_Start()方法中初始化ActiveRecord配置

 1 using Castle.ActiveRecord; 2 using Castle.ActiveRecord.Framework; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Web; 7 using System.Web.Mvc; 8 using System.Web.Optimization; 9 using System.Web.Routing;10 11 namespace ActiveRecord.Demo12 {13   public class MvcApplication : System.Web.HttpApplication14   {15     protected void Application_Start()16     {17       AreaRegistration.RegisterAllAreas();18       FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);19       RouteConfig.RegisterRoutes(RouteTable.Routes);20       BundleConfig.RegisterBundles(BundleTable.Bundles);21 22       InitActiveRecord();23     }24 25     private void InitActiveRecord()26     {27       IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource;28       ActiveRecordStarter.Initialize(source,29         typeof(Castle.ActiveRecord.DB1.Models.DB1ActiveRecordBase),30         typeof(Castle.ActiveRecord.DB1.Models.Post),31 32         typeof(Castle.ActiveRecord.DB2.Models.DB2ActiveRecordBase),33         typeof(Castle.ActiveRecord.DB2.Models.Post)34         );35     }36   }37 }

7.配置现在完成,增加控制器视图,进行两个数据库中Post表数据的显示

  • 新增控制器DB1PostController
 1 using Castle.ActiveRecord.DB1.Models; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Mvc; 7  8 namespace ActiveRecord.Demo.Controllers 9 {10   public class DB1PostController : Controller11   {12     // GET: DB1Post13     public ActionResult Index()14     {15       IEnumerable<Post> post = Post.FindAllBy();16 17       return View(post);18     }19   }20 }

对应视图:
 1 @using Castle.ActiveRecord.DB1.Models 2 @model IEnumerable<Post> 3 @{ 4   ViewBag.Title = "DB1-Post"; 5 } 6  7 <h2>DB1-Post</h2> 8 <table class="table"> 9   <thead>10     <tr>11       <th>主键</th>12       <th>主题</th>13       <th>内容</th>14       <th>创建日期</th>15     </tr>16   </thead>17   <tbody>18     @foreach (var m in Model)19     {20       <tr>21         <td>@m.Id</td>22         <td>@m.Subject</td>23         <td>@m.Text</td>24         <td>@m.DateAdded</td>25       </tr>26     }27   </tbody>28 </table>

  • 新增控制器DB2PostController
 1 using Castle.ActiveRecord.DB2.Models; 2 using System; 3 using System.Collections.Generic; 4 using System.Linq; 5 using System.Web; 6 using System.Web.Mvc; 7  8 namespace ActiveRecord.Demo.Controllers 9 {10   public class DB2PostController : Controller11   {12     // GET: DB2Post13     public ActionResult Index()14     {15       IEnumerable<Post> post = Post.FindAllBy();16 17       return View(post);18     }19   }20 }

对应视图:
 1 @using Castle.ActiveRecord.DB2.Models 2 @model IEnumerable<Post> 3 @{ 4   ViewBag.Title = "DB2-Post"; 5 } 6  7 <h2>DB2-Post</h2> 8 <table class="table"> 9   <thead>10     <tr>11       <th>主键</th>12       <th>主题</th>13       <th>内容</th>14       <th>创建日期</th>15     </tr>16   </thead>17   <tbody>18     @foreach (var m in Model)19     {20       <tr>21         <td>@m.Id</td>22         <td>@m.Subject</td>23         <td>@m.Text</td>24         <td>@m.DateAdded</td>25       </tr>26     }27   </tbody>28 </table>

8.修改_Layout.cshtml,添加链接进行DB1数据库、DB2数据库表Post数据的显示

1 <li>@Html.ActionLink("DB1-Post", "Index", "DB1Post")</li>2 <li>@Html.ActionLink("DB2-Post", "Index", "DB2Post")</li>

运行后效果:




韩国旅游去哪好韩国旅游团报价优惠韩国蜜月旅游行程参考韩国特价旅游多少钱到韩国游费用报价大全2015深圳锦绣中华圣诞节夜场活动门票预订价格?锦绣中华圣诞夜场门票多少钱? 2015深圳世界之窗圣诞节夜场活动门票预订价格?世界之窗圣诞夜场门票多少钱? 2015深圳欢乐谷圣诞节夜场活动门票预订价格?深圳欢乐谷圣诞夜场门票多少钱? 2015深圳欢乐谷圣诞节夜场活动时间?深圳欢乐谷圣诞夜场几时开始? 普吉岛SPA蜜月游 享受海岛风情的愉悦和惬意 西藏旅游线路推荐 塞班岛旅游买什么好? 蜜月旅游 新婚蜜月旅游 夫妻蜜月旅游 旅游 笔架山小花紫薇花开艳丽 深圳一日游哪里好玩 深圳一日游景点攻略 美好六月笔架山绿意浓浓 深圳周末去哪玩 深圳南澳湾吃喝玩乐攻略 深圳海上田园地址_深圳海上田园在哪里_深圳海上田园怎么去 马尔代夫旅游购物与货币知识 马尔代夫满月岛旅游攻略_满月岛地理位置|房型|最佳旅游时间|注意事项 如梦如幻 马尔代夫水下餐厅“伊特哈” TLRMH151P(F) Datasheet TLRMH151P(F) Datasheet TLRMH151P(T) Datasheet TLRMH151P(T) Datasheet TLRMH156 Datasheet TLRMH156 Datasheet 春熙路到锦里 春熙路到锦里 春熙路到锦里 锦里到宽窄巷子 锦里到宽窄巷子 锦里到宽窄巷子 梵净山在哪里 梵净山在哪里 梵净山在哪里