using System; using Castle.ActiveRecord;[ActiveRecord( " Companies " )] public Class Company : ActiveRecordBase { }
上面的代码就定义了一个AR实体,ActiveRecord特性就代表该类对应数据库中的哪张表,如果类名和表明相同,那么可以省略不写 [ActiveRecord()] public class Companies : ActiveRecordBase { }
2、PrimaryKey(声明主键) private int _id;[PrimaryKey] public int Id { get{ return _id;} set{_id = value;}}
如果数据库中主键的字段名和在AR实体中定义的属性名不同的话,则需指定主键属性对应于数据库中的字段名。
3、Property(特性)每个Property对应数据库中的一个非主键的字段,和PrimaryKey的用法一样。 private string _cname;[Property] public string Cname { get{ return _cname;} set{_cname = value;}}
或
private string _cname;[Property( " companies_Cname " )] public string Cname { get{ return _cname;} set{_cname = value;}}
4、BelongsTo(属于),HasMany(包含)
BelongsTo用于描述实体间多对一(Many to One)的关系,HasMany用于描述一对多的实体间关系,例如博客,每个博客可以有多篇文章,文章和博客之间的关系就是多对一,反之博客和文章之间的关系就是一对多。Many To One: [ActiveRecord] public class Post : ActiveRecordBase { private Blog blog; [BelongsTo("post_blogid")] public Blog Blog { get { return blog; } set { blog = value; } }}
One To Many:
[ActiveRecord( " Blogs " )] public class Blog : ActiveRecordBase { private IList _posts; [HasMany(typeof(Post), Table="posts", ColumnKey="post_blogid")] public IList Posts { get { return _posts; } set { _posts = value; } }}
HasMany 特性中必须指明子表的名称,以及关联的外键字段名。
5、HasAndBelongsToMany 此特性用于描述实体间多对多(Many To Many)的关系。随着企业的集团化,企业中员工和部门之间的关系就是一个典型的Many To Many关系,一个部门可以有多个员工,一个员工可以属于几个部门。我们在做企业组织机构的时候首先要建企业部门和企业员工这两个对象表,然后这两张表之间的联系通过另外一张表来描述,这个表称为关系表。描述多对多关系是需注意几个必须指定的属性:Table:用于指定描述两个对象表之间关系的表名。ColumnRef:关联于另外一个对象表的字段名,这个字段名是关系表中的。ColumnKey:关系表中对应自己的字段名 [HasAndBelongsToMany( typeof (AR.Model.People),Table = " people_companies " ,ColumnRef = " people_id " , ColumnKey = " company_id " )] public IList People { get{ return _people;} set{_people = value;} }
[HasAndBelongsToMany( typeof (Company), Table = " people_companies " , ColumnRef = " company_id " , ColumnKey = " people_id " )] public IList Companies { get { return _companies; } set { _companies = value; } }
第二部分、各种映射关系的操作 这几种操作,TerryLee的博客中已经写的很清楚了,我就不再多说了,我只讲一个Many To Many的关系,因为当时我按照TerryLee的博客上写的做调试没通过,后来自己重做的。 TerryLee的文章: Many To Many: 1、数据库 -- 1.部门表 create table companies( id int identity , pid int null , cname varchar ( 50 ) null , type varchar ( 20 ) null , primary key (id)) go -- 2.员工表 create table people( id int identity , pname varchar ( 20 ) null , primary key (id)) go -- 3.部门员工对照表 create table people_companies( people_id int not null , company_id int not null ) go
2、创建AR实体 Company using System;using System.Collections;using Castle.ActiveRecord;using Castle.ActiveRecord.Queries;namespace AR.Model{ /**//// <summary> /// Company 的摘要说明。 /// </summary> [ActiveRecord("companies")] public class Company : Castle.ActiveRecord.ActiveRecordBase { private int _id; private int _pid; private string _cname; private string _type; private IList _people; public Company() { } public Company(string name) { this._cname = name; } [PrimaryKey] public int Id { get{ return _id;} set{_id = value;} } [Property] public int Pid { get{ return _pid;} set{_pid = value;} } [Property] public string Cname { get{ return _cname;} set{_cname = value;} } [Property] public string Type { get{ return _type;} set{_type = value;} } [HasAndBelongsToMany(typeof(AR.Model.People),Table="people_companies",ColumnRef="people_id", ColumnKey="company_id" )] public IList People { get{ return _people;} set{_people = value;} } public static void DeleteAll() { ActiveRecordBase.DeleteAll(typeof(Company)); } public static Company[] FindAll() { return ((Company[])(ActiveRecordBase.FindAll(typeof(Company)))); } public static Company Find(int id) { return ((Company)(ActiveRecordBase.FindByPrimaryKey(typeof(Company), id))); } }}
People using System;using System.Collections;using Castle.ActiveRecord;namespace AR.Model{ /**//// <summary> /// People 的摘要说明。 /// </summary> [ActiveRecord("people")] public class People : ActiveRecordBase { private int _id; private String _name; private IList _companies; public People() { _companies = new ArrayList(); } [PrimaryKey] public int Id { get { return _id; } set { _id = value; } } [Property("pname")] public string Name { get { return _name; } set { _name = value; } } [HasAndBelongsToMany( typeof(Company), Table="people_companies", ColumnRef="company_id", ColumnKey="people_id" )] public IList Companies { get { return _companies; } set { _companies = value; } } public static void DeleteAll() { ActiveRecordBase.DeleteAll(typeof(People)); } public static People[] FindAll() { return ((People[])(ActiveRecordBase.FindAll(typeof(People)))); } public static People Find(int id) { return ((People)(ActiveRecordBase.FindByPrimaryKey(typeof(People), id))); } }}
3、ADD Of Many To Many // 添加 private void Button1_Click( object sender, System.EventArgs e) { //新增一个员工,让他同时属于两个部门 People per = new People(); per.Name = "pw"; ArrayList al = new ArrayList(); al.Add(Company.Find(2)); al.Add(Company.Find(3)); per.Companies = al; per.Save(); }
4、Update of Many To Many // 更新 private void Update_Click( object sender, System.EventArgs e) { //查找一个已经存在的员工,更新他所在的部门 People per = People.Find(1); //删除他以前所在的部门再添加,如果不删除以前的部门,就把下面第一句去掉 per.Companies.Clear(); per.Companies.Add(Company.Find(6)); per.Companies.Add(Company.Find(7)); per.Save(); }
5、Delete of Many To Many // 删除 private void Delete_Click( object sender, System.EventArgs e) { //此时只删除了People表和companies_people表的相应的纪录,因Company表的纪录还可能跟其他的纪录有关联,所以不能删除 //如果只是想删除员工和部门的对应关系,可以用per.clear();或per.Remove(); People per = People.Find(1); using(TransactionScope trans = new TransactionScope()) { try { per.Delete(); trans.VoteCommit(); } catch(Exception) { trans.VoteRollBack(); } } }
关于AR的映射及其基本操作就说到这里,这里再次感谢TerryLee,他的文章对我帮助很大,同时很渴望和对AR有兴趣的朋友多多交流.
前两篇:(一):(二):