本來打算一口氣把Castle.ActiveRecord的學習筆記寫完,沒想到前段時間太忙了,一放就放了半個月,現在繼續未完成的學習筆記吧!
這篇學習筆記主要介紹ActiveRecord如何處理one-to-many的映射,對於many-to-one映射只需把one-to-many的處理過程反過來就可以了。本文涉及了兩個實體類User、Address,兩個類的關系是一對多,下面是這兩個類的實體關系圖:
主要內容:
1.編寫數據庫
2.HasMany和BelongsTo屬性說明
3.編寫實體類
4.編寫表示層調用代碼
一、編寫數據庫 --User類對應的數據表
Create Table [Users] ( [ID] Int Identity(1,1) Primary Key, [LoginName] Varchar(50) not null, [Password] Varchar(20) not null )
--Address類對應的數據表
Create Table [Address] ( ID int identity(1,1) primary key, Province Varchar(50), City Varchar(50), uID int FOREIGN KEY REFERENCES Users (ID) )
二、HasMany和BelongsTo屬性說明
HasMany屬性:此屬性用於代替NHibernate配置文件中的<many-to-one>標簽;該屬性用在"一"的類中(父對象,本文為User類),指出與"多"的類(級聯對象,本文為Address類)的對應關系.具有以下幾個主要子屬性:
1.Type:指出關聯對象的類名,相當於<many-to-one>標簽中的class屬性;
2.Table:指出關聯對象的類對應的數據表(本文的數據表Address);
3.ColumnKey:指出關聯對象對應的數據表中指向主類數據表的字段名(本文數據表Address中的字段uID);
4.Cascade:指明哪些操作會從父對象級聯到關聯的對象,相當於<many-to-one>標簽中的cascade屬性.該屬性值應為CascadeEnum枚舉值之一:
a).All:表示父對象的任何操作都會關聯到級聯對象;
b).Delete:表示只有對父對象進行刪除操作時才會關聯到級聯對象;
c).SaveUpdate:表示只有對父對象進行保存、更新操作時才會關聯到級聯對象;
d).None(默認值):表示對父對象的任何操作都不會關聯到級聯對象;
5.Lazy:指出是否延遲加載級聯對象,其屬性值為true或false,二者選一;
6.Where:指定一個附加SQL的Where子句,這裡應該寫HQL語句;
7.OrderBy:指定排序方式,這裡應該寫HQL語句;
8.Inverse:指定父對象是否級聯到子對象;
BelongsTo屬性:此屬性用於代替NHibernate配置文件中表示<map>、<set>、<list>、<bag>之類的標簽;該屬性用在父對象中,指出與級聯對象的對應關系.具有以下幾個主要子屬性:
1.Cascad:指出是否級聯操作,其值於HasMany屬性中的Cascade屬性一樣;
2.Column:指出數據表中指向父對象數據表的字段名;
3.Insert:是否允許插入;
4.Update:是否允許更新;
5.OuterJoin:是否允許外聯抓取,相當於<many-to-one>標簽中的outerjoin屬性,其值應為OuterJoinEnum枚舉值之一:
a).Auto(默認值):使用外連接抓取關聯(對象),如果被關聯的對象沒有代理(proxy) ;
b).True:一直使用外連接來抓取關聯;
c).False:永遠不使用外連接來抓取關聯;
三、編寫類體類
1.編寫實體類:
User: /**////[email protected] ///2006-09-11 /**//*數據庫腳本 Create Table [Users] ( [ID] Int Identity(1,1) Primary Key, [LoginName] Varchar(50) not null, [Password] Varchar(20) not null ) */ using System; using System.Collections.Generic; using System.Text; using Castle.ActiveRecord; using System.Collections; namespace OneToMany { [ActiveRecord("Users")] public class User : ActiveRecordBase { private int intID; private string strName; private string strPassword; private IList AddressList = new ArrayList(); [PrimaryKey(PrimaryKeyType.Identity, "ID")] public int Id { get { return intID; } set { intID = value; } } [Property("LoginName")] public string Name { get { return strName; } set { strName = value; } } [Property] public string Password { get { return strPassword; } set { strPassword = value; } } [HasMany(typeof(Address), Table = "Address", ColumnKey = "uID")] public IList Address { get { return AddressList; } set { AddressList = value; } } } }
2.編寫實體類:
Address /**////[email protected] ///2006-09-11 /**//*數據庫腳本 Create Table [Address] ( ID int identity(1,1) primary key, Province Varchar(50), City Varchar(50), uID int FOREIGN KEY REFERENCES Users (ID) ) */ using System; using System.Collections.Generic; using System.Text; using Castle.ActiveRecord; namespace OneToMany { [ActiveRecord("Address")] public class Address :ActiveRecordBase { private int intID; private string strProvince; private string strCity; private User objUser; [PrimaryKey(PrimaryKeyType.Native,"ID")] public int Id { get { return intID; } set { intID = value; } } /**//// <summary> /// 省 /// </summary> [Property] public string Province { get { return strProvince; } set { strProvince = value; } } /**//// <summary> /// 城市 /// </summary> [Property] public string City { get { return strCity; } set { strCity = value; } } [BelongsTo("uId")] public User User { get { if (objUser == null) { objUser = new User(); } return objUser; } set { objUser = value; } } } }
3.編寫配置文件(與以前一樣)
四、編寫表示層調用代碼(只列舉新增的代碼)
private void btnAddNewUser_Click(object sender, EventArgs e) { IConfigurationSource source = System.Configuration.ConfigurationSettings.GetConfig("activerecord") as IConfigurationSource; ActiveRecordStarter.Initialize(source, typeof(User), typeof(Address)); //使用事務處理 using (TransactionScope tran = new TransactionScope()) { try { User objUser = new User(); objUser.Name = "jailu"; objUser.Password = "123456789"; objUser.Save(); Address objAddress; for (int i = 0; i < 5; i++) { objAddress = new Address(); objAddress.Province = "Province" + i.ToString(); objAddress.City = "City" + i.ToString(); objAddress.User = objUser; objAddress.Save(); } tran.VoteCommit(); //執行事務 MessageBox.Show("Success!"); } catch (Exception ex) { tran.VoteRollBack(); //若出現異常,回滾事務 MessageBox.Show("Fail!" + ex.Message); } } }