Castle.ActiveRecord多数据库配置
最近使用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表数据的显示
新增控制器DB1PostController
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>
新增控制器DB2PostController
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>
运行后效果:
Castle.ActiveRecord多数据库配置的更多相关文章
- Castle连接多数据库配置
ActiveRecord 的多数据库配置基本沿袭了 NHibernate 的思想,只不过在配置文件结构上作了些调整.1. 采用继承方式,归纳使用同一数据库的类型.比如 A.B.C.D.E 中 A.B连 ...
- ActiveRecord多数据库配置
ActiveRecord 的多数据库配置基本沿袭了 NHibernate 的思想,只不过在配置文件结构上作了些调整.NHibernate的配置也是基于配置得来的,配置多个SessionFactory传 ...
- Castle ActiveRecord学习(二)配置、引用、程序启动
来源:http://www.cnblogs.com/zxj159/p/4082987.html 配置数据库驱动: Model层引用:Castle.ActiveRecord.dll.NHibernate ...
- Castle ActiveRecord配置中需要注意的地方
关于Castle 的开发可参考李会军老师的Castle 开发系列文章,里面有关于ActiveRecord学习实践系列和Castle IOC容器系列两个部分,是比较好的教程. 这里主要说明在Castle ...
- 记录Castle ActiveRecord访问Sqlite的配置
1.ActiveRecord配置文件ARConfig.xml,并将配置文件的“生成操作”改成“嵌入的资源” <?xml version=; i < ; i++) { ...
- Castle ActiveRecord框架学习(一)
一.Active Record(活动记录)模式 Active Record是业务逻辑层中(<企业应用架构模式>将该模式归为数据源模式)常用的一种框架模式,尤其在底层数据库模型匹配业务模型时 ...
- Castle ActiveRecord学习(三)数据映射及特性描述
Model中的Demo: using Castle.ActiveRecord; using Castle.ActiveRecord.Queries; using System; using Syste ...
- Castle ActiveRecord学习(一)简介
简介 来源:http://www.cnblogs.com/zxj159/p/4082987.html 一.Active Record(活动记录)模式 Active Record是业务逻辑层中(< ...
- Castle ActiveRecord框架学习(二):快速搭建简单博客网站
一.数据库 1.数据表 Category:类别标签表(字段Type=1为类别,Type=2为标签) Category_Post:类别标签与文章中间表 Post:文章表 Comment:评论表 2.数据 ...
随机推荐
- like语句百分号前置会使用到索引吗?
like语句百分号前置会使用到索引吗? 前几天看了这篇文章:谈SQL Server对like '%关键词%' 处理时的索引利用问题 看完了之后,我很想知道这篇文章是不是临时工写的?还是网站的主人写的, ...
- 《深入理解Java虚拟机》Java内存区域与内存溢出异常
注:“蓝色加粗字体”为书本原语 先来一张JVM运行时数据区域图,再接下来一一分析各区域功能: 程序计数器 程序计数器(program Counter Register)是一块较小的内存空间,它可以 ...
- 如何理解T-SQL中Merge语句(二)
写在前面的话:上一篇写了如何理解T-SQL中Merge语句,基本把Merge语句要讲的给讲了,在文章的后面,抛出了几个结,当时没有想明白怎么去用文字表达,这一篇就来解答一下这几个结,又是一篇“天马行空 ...
- 小学徒成长系列—StringBuilder & StringBuffer关键源码解析
在前面的博文<小学徒成长系列—String关键源码解析>和<小学徒进阶系列—JVM对String的处理>中,我们讲到了关于String的常用方法以及JVM对字符串常量Strin ...
- [ASP.NET MVC 小牛之路]06 - 使用 Entity Framework
在家闲着也是闲着,继续写我的[ASP.NET MVC 小牛之路]系列吧.在该系列的上一篇博文中,在显示书本信息列表的时候,我们是在程序代码中手工造的数据.本文将演示如何在ASP.NET MVC中使用E ...
- Js toString()方法笔记
一.前言 toString()用于将该对象以字符串(string)的形式返回,该方法属于Object对象;也就是说对象都能够使用此方法. 之前所接触的是array.toString()数组转换为字符串 ...
- ie6 z-index不起作用的解决方法
一.概念 z-index伴随着层的概念产生的.网页中,层的概念与photoshop或是flash中层的概念是一致的.熟悉photoshop或是flash的应该知道,层级越高(图层越靠上),越在上面显示 ...
- Disk IO Performance
一,使用 Performance counter 监控Disk IO问题 1,Physical Disk vs. Logical Disk Windows可以在一个Physical Disk上划出若干 ...
- 上传伪技术~很多人都以为判断了后缀,判断了ContentType,判断了头文件就真的安全了。是吗?
今天群里有人聊图片上传,简单说下自己的经验(大牛勿喷) 0.如果你的方法里面是有指定路径的,记得一定要过滤../,比如你把 aa文件夹设置了权限,一些类似于exe,asp,php之类的文件不能执行,那 ...
- WPF MVVM框架下,VM界面写控件
MVVM正常就是在View页面写样式,ViewModel页面写逻辑,但是有的时候纯在View页面写样式并不能满足需求.我最近的这个项目就遇到了,因此只能在VM页面去写样式控件,然后绑定到View页面. ...