最近使用Castle.ActiveRecord框架,网上关于多数据支持的文章很少,因此有了这篇博文的产生。
开发工具VS2015,Sql Server2008R2
新建数据库,数据初始化脚本如下:
--新建数据库Castle.ActiveRecord.DB1
CREATE DATABASE [Castle.ActiveRecord.DB1];
GO
USE [Castle.ActiveRecord.DB1]
GO
/****** Object: Table [dbo].[Post] Script Date: 09/25/2016 16:46:21 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Post](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Subject] [nvarchar](64) NULL,
[Text] [nvarchar](1024) NULL,
[DateAdded] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
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'
GO
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'
GO
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'
GO
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'
GO
SET IDENTITY_INSERT [dbo].[Post] ON
INSERT [dbo].[Post] ([Id], [Subject], [Text], [DateAdded]) VALUES (1, N'新闻', N'最新新闻内容', '2016-09-01')
INSERT [dbo].[Post] ([Id], [Subject], [Text], [DateAdded]) VALUES (2, N'音乐', N'流行音乐', '2016-09-02')
SET IDENTITY_INSERT [dbo].[Post] OFF
--新建数据库Castle.ActiveRecord.DB2
CREATE DATABASE [Castle.ActiveRecord.DB2];
GO
USE [Castle.ActiveRecord.DB2]
GO
/****** Object: Table [dbo].[Post] Script Date: 09/25/2016 16:53:05 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Post](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Subject] [nvarchar](64) NULL,
[Text] [nvarchar](1024) NULL,
[DateAdded] [datetime] NULL,
PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
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'
GO
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'
GO
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'
GO
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'
GO
SET IDENTITY_INSERT [dbo].[Post] ON
INSERT [dbo].[Post] ([Id], [Subject], [Text], [DateAdded]) VALUES (1, N'小说', N'纪实小说', '2016-09-03')
INSERT [dbo].[Post] ([Id], [Subject], [Text], [DateAdded]) VALUES (2, N'电脑', N'超极本', '2016-09-04')
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抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Castle.ActiveRecord.DB1.Models
{
public abstract class DB1ActiveRecordBase : ActiveRecordBase
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Castle.ActiveRecord.DB1.Models
{
[ActiveRecord("Post")]
public class Post : DB1ActiveRecordBase
{
/// <summary>
/// 自增主键
/// </summary>
[PrimaryKey(PrimaryKeyType.Identity, Column = "Id")]
public int Id { set; get; }
/// <summary>
/// 主题
/// </summary>
[Property("Subject")]
public string Subject { set; get; }
/// <summary>
/// 内容
/// </summary>
[Property("Text")]
public string Text { set; get; }
/// <summary>
/// 添加日期
/// </summary>
[Property("DateAdded")]
public DateTime DateAdded { set; get; }
public static IEnumerable<Post> FindAllBy()
{
return FindAll(typeof(Post)).Cast<Post>(); ;
}
}
}
Castle.ActiveRecord.DB2.Models类库添加DB2ActiveRecordBase抽象类文件,继承自ActiveRecordBase基类
添加Post类,继承自DB2ActiveRecordBase抽象类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Castle.ActiveRecord.DB2.Models
{
public abstract class DB2ActiveRecordBase : ActiveRecordBase
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Castle.ActiveRecord.DB2.Models
{
[ActiveRecord("Post")]
public class Post : DB2ActiveRecordBase
{
/// <summary>
/// 自增主键
/// </summary>
[PrimaryKey(PrimaryKeyType.Identity, Column = "Id")]
public int Id { set; get; }
/// <summary>
/// 主题
/// </summary>
[Property("Subject")]
public string Subject { set; get; }
/// <summary>
/// 内容
/// </summary>
[Property("Text")]
public string Text { set; get; }
/// <summary>
/// 添加日期
/// </summary>
[Property("DateAdded")]
public DateTime DateAdded { set; get; }
public static IEnumerable<Post> FindAllBy()
{
return FindAll(typeof(Post)).Cast<Post>(); ;
}
}
}
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配置
using Castle.ActiveRecord;
using Castle.ActiveRecord.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace ActiveRecord.Demo
{
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
InitActiveRecord();
}
private void InitActiveRecord()
{
IConfigurationSource source = System.Configuration.ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
ActiveRecordStarter.Initialize(source,
typeof(Castle.ActiveRecord.DB1.Models.DB1ActiveRecordBase),
typeof(Castle.ActiveRecord.DB1.Models.Post),
typeof(Castle.ActiveRecord.DB2.Models.DB2ActiveRecordBase),
typeof(Castle.ActiveRecord.DB2.Models.Post)
);
}
}
}
7.配置现在完成,增加控制器视图,进行两个数据库中Post表数据的显示
using Castle.ActiveRecord.DB1.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ActiveRecord.Demo.Controllers
{
public class DB1PostController : Controller
{
// GET: DB1Post
public ActionResult Index()
{
IEnumerable<Post> post = Post.FindAllBy();
return View(post);
}
}
}
对应视图:
@using Castle.ActiveRecord.DB1.Models
@model IEnumerable<Post>
@{
ViewBag.Title = "DB1-Post";
}
<h2>DB1-Post</h2>
<table class="table">
<thead>
<tr>
<th>主键</th>
<th>主题</th>
<th>内容</th>
<th>创建日期</th>
</tr>
</thead>
<tbody>
@foreach (var m in Model)
{
<tr>
<td>@m.Id</td>
<td>@m.Subject</td>
<td>@m.Text</td>
<td>@m.DateAdded</td>
</tr>
}
</tbody>
</table>
using Castle.ActiveRecord.DB2.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace ActiveRecord.Demo.Controllers
{
public class DB2PostController : Controller
{
// GET: DB2Post
public ActionResult Index()
{
IEnumerable<Post> post = Post.FindAllBy();
return View(post);
}
}
}
对应视图:
@using Castle.ActiveRecord.DB2.Models
@model IEnumerable<Post>
@{
ViewBag.Title = "DB2-Post";
}
<h2>DB2-Post</h2>
<table class="table">
<thead>
<tr>
<th>主键</th>
<th>主题</th>
<th>内容</th>
<th>创建日期</th>
</tr>
</thead>
<tbody>
@foreach (var m in Model)
{
<tr>
<td>@m.Id</td>
<td>@m.Subject</td>
<td>@m.Text</td>
<td>@m.DateAdded</td>
</tr>
}
</tbody>
</table>
8.修改_Layout.cshtml,添加链接进行DB1数据库、DB2数据库表Post数据的显示
<li>@Html.ActionLink("DB1-Post", "Index", "DB1Post")</li>
<li>@Html.ActionLink("DB2-Post", "Index", "DB2Post")</li>
运行后效果: