您的位置:亚洲必赢 > 军事新闻 >   初始化配置也还是和以前的版本使用方式一

  初始化配置也还是和以前的版本使用方式一

2019-12-02 23:35

    3.新建MVC项目ActiveRecord.德姆o,新建类库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引用。

图片 1

  

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

protected void Application_Start(object sender, EventArgs e)
{
    IConfigurationSource source = ConfigurationManager.GetSection("activerecord") as IConfigurationSource;
    //ActiveRecordStarter.Initialize(source, typeof(Entity.UserInfo));
    ActiveRecordStarter.Initialize(Assembly.Load("Entity"), source);
}

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

图片 2

    [Property("Sex", Length = 50)]
    public string Sex { get; set; }

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 }

 

运行后效果:

图片 3

图片 4

  由于涉及映射对象世袭于ActiveRecordBase<T>泛型版本,因而能够直接基于关系映射对象使用数据悠久化操纵行为,和老版本的使用模式是如出一辙的。

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 }

  前后大概七年没动用过Castle.ActiveRecord做ORM了,也近八年未有关心Castle的版本变化了,近来关切了意识众多地点都改成了。明天在四个现有的小品种中尝试增加Castle.ActiveRecord来做多少漫长化操作,早前无需做ProxyFactory配置,今后必定要ProxyFactory配置。使用到了NHibernate.ByteCode.Castle.dll组件。

 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 : 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 }

    [Property("Name", NotNull = true)]
    public string Name { get; set; }
}

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 }

 

5.MVC项目ActiveRecord.德姆o项目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>

图片 5

 

眼下选择Castle.ActiveRecord框架,互连网有关多数据支撑的稿子比较少,由此有了那篇博文的爆发。

  1. #### 开拓工具VS二〇一四,Sql Server2009Tiggo2

  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'最新音讯内容', '二〇一四-09-01')33 INSERT [dbo].[Post] ([Id], [Subject], [Text], [DateAdded]卡塔尔(英语:State of Qatar) VALUES (2, N'音乐', N'流行音乐', '2015-09-02'卡塔尔(英语:State of Qatar)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'纪实小说', '贰零壹伍-09-03'卡塔尔(قطر‎68 INSERT [dbo].[Post] ([Id], [Subject], [Text], [DateAdded]) VALUES (2, N'电脑', N'超极本', '2016-09-04') 69 SET IDENTITY_INSERT [dbo].[Post] OFF

[ActiveRecord("UserInfo")]
public class UserInfo : ActiveRecordBase<UserInfo>
{
    [PrimaryKey(PrimaryKeyType.Identity, "ID")]
    public int ID { get; set; }

   至于ProxyFactory的实际职能暂无深远钻研,也不领会AHighlander为何会在新本子中加上那一个效果,作者想应该是想通过NHibernate对Castle的ATucson进行一些调整,暂风尚马时间切磋,期望有色金属研讨所究过的恋人分享,多谢。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        UserInfo info = new UserInfo();
        info.Name = "zhangsan";
        info.Sex = "男";
        //插入info数据到数据库中所映射的关系表
        info.Create();
    }
}

  别的的行使基本上未有成形,下边是叁个ORM的对象配置。

  <activerecord>
    <config>
      <add key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
      <add key="dialect" value="NHibernate.Dialect.MsSql2000Dialect" />
      <add key="connection.provider" value="NHibernate.Connection.DriverConnectionProvider" />
      <add key="connection.connection_string" value="Data Source=SQL05;Initial Catalog=testdb;uid=sa;pwd=12345;" />
      <add key="proxyfactory.factory_class" value="NHibernate.ByteCode.Castle.ProxyFactoryFactory, NHibernate.ByteCode.Castle"/>
    </config>
  </activerecord>

 

  ActoiveRecord的陈设文件有了猛烈的变通,供给配备ProxyFactory工夫成就长久化操纵,详细安排如下:

本文由亚洲必赢发布于军事新闻,转载请注明出处:  初始化配置也还是和以前的版本使用方式一

关键词: 亚洲必赢 .NET技术 NHibernate