数据库中有商品表

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

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

图片 1

    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引用。

图片 2

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’注册IP’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Members’, @level2type=N’COLUMN’, @level2name=N’RegIP’

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 : ActiveRecordBase
10     {
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 : DB2ActiveRecordBase
11     {
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>

图片 3

数据库模型如下:

    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

对应视图:
 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 : Controller
    11 {
    12 // GET: DB2Post
    13 public ActionResult Index()
    14 {
    15 IEnumerable post = Post.FindAllBy();
    16
    17 return View(post);
    18 }
    19 }
    20 }

GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ORDERS_REFERENCE_MEMBERS]’) AND parent_object_id = OBJECT_ID(N'[dbo].[Orders]’))
ALTER TABLE [dbo].[Orders]  WITH CHECK ADD  CONSTRAINT [FK_ORDERS_REFERENCE_MEMBERS] FOREIGN KEY([UserID])
REFERENCES [dbo].[Members] ([UserID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ORDERDET_REFERENCE_ORDERS]’) AND parent_object_id = OBJECT_ID(N'[dbo].[OrderDetail]’))
ALTER TABLE [dbo].[OrderDetail]  WITH CHECK ADD  CONSTRAINT [FK_ORDERDET_REFERENCE_ORDERS] FOREIGN KEY([OrderID])
REFERENCES [dbo].[Orders] ([OrderID])
GO
IF NOT EXISTS (SELECT * FROM sys.foreign_keys WHERE object_id = OBJECT_ID(N'[dbo].[FK_ORDERDET_REFERENCE_PRODUCTS]’) AND parent_object_id = OBJECT_ID(N'[dbo].[OrderDetail]’))
ALTER TABLE [dbo].[OrderDetail]  WITH CHECK ADD  CONSTRAINT [FK_ORDERDET_REFERENCE_PRODUCTS] FOREIGN KEY([ProductID])
REFERENCES [dbo].[Products] ([ProductID])

对应视图:
 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 : Controller
    11 {
    12 // GET: DB2Post
    13 public ActionResult Index()
    14 {
    15 IEnumerable post = Post.FindAllBy();
    16
    17 return View(post);
    18 }
    19 }
    20 }

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

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

图片 5

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[OrderDetail]’) AND type in (N’U’))
BEGIN
CREATE TABLE [dbo].[OrderDetail](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [OrderID] [int] NULL,
    [ProductID] [int] NULL,
    [Quantity] [int] NULL,
    [UnitPrice] [money] NULL,
 CONSTRAINT [PK_ORDERDETAIL] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’订单号’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’OrderDetail’, @level2type=N’COLUMN’, @level2name=N’OrderID’

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.Demo
12 {
13     public class MvcApplication : System.Web.HttpApplication
14     {
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 }

运转后效果:

图片 6

图片 7

GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Products]’) AND type in (N’U’))
BEGIN
CREATE TABLE [dbo].[Products](
    [ProductID] [int] IDENTITY(1,1) NOT NULL,
    [ProductName] [nvarchar](100) NOT NULL,
    [Quantity] [int] NULL,
    [Pic] [varchar](100) NULL,
    [Description] [nvarchar](500) NULL,
    [Status] [tinyint] NULL,
    [AddTime] [datetime] NULL,
 CONSTRAINT [PK_PRODUCTS] PRIMARY KEY CLUSTERED 
(
    [ProductID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [AK_KEY_2_PRODUCTS] UNIQUE NONCLUSTERED 
(
    [ProductName] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Orders]’) AND type in (N’U’))
BEGIN
CREATE TABLE [dbo].[Orders](
    [OrderID] [int] IDENTITY(1,1) NOT NULL,
    [UserID] [int] NULL,
    [WorkFlowID] [uniqueidentifier] NULL,
    [TotalPrice] [money] NULL,
    [OrderDate] [datetime] NULL DEFAULT (getdate()),
    [Status] [tinyint] NULL,
 CONSTRAINT [PK_ORDERS] PRIMARY KEY CLUSTERED 
(
    [OrderID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’订单号’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Orders’, @level2type=N’COLUMN’, @level2name=N’OrderID’

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 : Controller
    11 {
    12 // GET: DB1Post
    13 public ActionResult Index()
    14 {
    15 IEnumerable post = Post.FindAllBy();
    16
    17 return View(post);
    18 }
    19 }
    20 }

近期选择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 GO
    10 SET QUOTED_IDENTIFIER ON
    11 GO
    12 CREATE TABLE [dbo].Post NOT NULL,
    14 [Subject] nvarchar NULL,
    15 [Text] nvarchar NULL,
    16 [DateAdded] [datetime] NULL,
    17 PRIMARY KEY CLUSTERED
    18 (
    19 [Id] ASC
    20 )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 GO
    23 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 GO
    25 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 GO
    27 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 GO
    29 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 GO
    31 SET IDENTITY_INSERT [dbo].[Post] ON
    32 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] OFF
    35
    36 –新建数据库Castle.ActiveRecord.DB2
    37 CREATE DATABASE [Castle.ActiveRecord.DB2];
    38 GO
    39
    40 USE [Castle.ActiveRecord.DB2]
    41 GO
    42 /** Object: Table [dbo].[Post] Script Date: 09/25/2016 16:53:05 **/
    43 SET ANSI_NULLS ON
    44 GO
    45 SET QUOTED_IDENTIFIER ON
    46 GO
    47 CREATE TABLE [dbo].Post NOT NULL,
    49 [Subject] nvarchar NULL,
    50 [Text] nvarchar NULL,
    51 [DateAdded] [datetime] NULL,
    52 PRIMARY KEY CLUSTERED
    53 (
    54 [Id] ASC
    55 )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 GO
    58 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 GO
    60 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 GO
    62 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 GO
    64 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 GO
    66 SET IDENTITY_INSERT [dbo].[Post] ON
    67 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

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’状态’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Orders’, @level2type=N’COLUMN’, @level2name=N’Status’

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>

图片 8

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 : ActiveRecordBase
10     {
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 : DB2ActiveRecordBase
11     {
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 }

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’数量’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’OrderDetail’, @level2type=N’COLUMN’, @level2name=N’Quantity’

  添加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 : ActiveRecordBase
10     {
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 : DB1ActiveRecordBase
11     {
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 }

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.Demo
12 {
13     public class MvcApplication : System.Web.HttpApplication
14     {
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 }

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’订单明细’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’OrderDetail’

近来使用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 GO
    10 SET QUOTED_IDENTIFIER ON
    11 GO
    12 CREATE TABLE [dbo].Post NOT NULL,
    14 [Subject] nvarchar NULL,
    15 [Text] nvarchar NULL,
    16 [DateAdded] [datetime] NULL,
    17 PRIMARY KEY CLUSTERED
    18 (
    19 [Id] ASC
    20 )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 GO
    23 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 GO
    25 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 GO
    27 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 GO
    29 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 GO
    31 SET IDENTITY_INSERT [dbo].[Post] ON
    32 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] OFF
    35
    36 –新建数据库Castle.ActiveRecord.DB2
    37 CREATE DATABASE [Castle.ActiveRecord.DB2];
    38 GO
    39
    40 USE [Castle.ActiveRecord.DB2]
    41 GO
    42 /** Object: Table [dbo].[Post] Script Date: 09/25/2016 16:53:05 **/
    43 SET ANSI_NULLS ON
    44 GO
    45 SET QUOTED_IDENTIFIER ON
    46 GO
    47 CREATE TABLE [dbo].Post NOT NULL,
    49 [Subject] nvarchar NULL,
    50 [Text] nvarchar NULL,
    51 [DateAdded] [datetime] NULL,
    52 PRIMARY KEY CLUSTERED
    53 (
    54 [Id] ASC
    55 )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 GO
    58 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 GO
    60 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 GO
    62 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 GO
    64 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 GO
    66 SET IDENTITY_INSERT [dbo].[Post] ON
    67 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

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 : Controller
    11 {
    12 // GET: DB1Post
    13 public ActionResult Index()
    14 {
    15 IEnumerable post = Post.FindAllBy();
    16
    17 return View(post);
    18 }
    19 }
    20 }

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’用户号码’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Orders’, @level2type=N’COLUMN’, @level2name=N’UserID’

对应视图:
 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>
对应视图:
 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>

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’订单日期’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Orders’, @level2type=N’COLUMN’, @level2name=N’OrderDate’

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

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

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’邮箱’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Members’, @level2type=N’COLUMN’, @level2name=N’Email’

运转后效果:

图片 9

图片 10

  添加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 : ActiveRecordBase
10     {
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 : DB1ActiveRecordBase
11     {
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 }

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’登陆密码’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Members’, @level2type=N’COLUMN’, @level2name=N’UserPwd’

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’总金额’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Orders’, @level2type=N’COLUMN’, @level2name=N’TotalPrice’

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’单价’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’OrderDetail’, @level2type=N’COLUMN’, @level2name=N’UnitPrice’

 其中要说的一个就是:订单表的行事流ID,大家前面要运用工作流,所以那几个工作流的ID是必须的。

大家要开支一个不难的B2C商城,能够一气呵成货物显示,购物车效率,订单流程就可以了,数据库大家运用SQLServer2005。数据库中有商品表,订单表,订单明细表,会员表就足以了,

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’会员表’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Members’

相应的SQL语句如下:

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’注册时间’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Members’, @level2type=N’COLUMN’, @level2name=N’Reg提姆e’

图片 11

PD图下载地址:http://files.cnblogs.com/sobaby/B2C_MVC.rar

图片 12图片 13Code
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[Members]’) AND type in (N’U’))
BEGIN
CREATE TABLE [dbo].[Members](
    [UserID] [int] IDENTITY(1,1) NOT NULL,
    [UserName] [nvarchar](50) NULL,
    [UserPwd] [varchar](32) NULL,
    [Email] [varchar](150) NULL,
    [RegIP] [varchar](15) NULL DEFAULT (getdate()),
    [RegTime] [datetime] NULL,
 CONSTRAINT [PK_MEMBERS] PRIMARY KEY CLUSTERED 
(
    [UserID] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY],
 CONSTRAINT [AK_KEY_2_MEMBERS] UNIQUE NONCLUSTERED 
(
    [UserName] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’用户号码’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Members’, @level2type=N’COLUMN’, @level2name=N’UserID’

GO
EXEC sys.sp_addextendedproperty @name=N’MS_Description’, @value=N’登录名’ ,@level0type=N’SCHEMA’, @level0name=N’dbo’, @level1type=N’TABLE’, @level1name=N’Members’, @level2type=N’COLUMN’, @level2name=N’UserName’

相关文章