












BelongsTo
BelongsTo 用来完成多对一的关联映射,例如在签名的文章中的例子Post—Blog的关系:
Cascade可以更加详细,以便激活insert、update等等:
[BelongsTo("post_blog_id", Cascade=CascadeEnum.All, Unique=true)]
public Blog Blog
{
get { return blog; }
set { blog = value; }
}
其中,Cascade是一个枚举类型,有以下成员:
|
None |
不进行任何级联操作,默认操作 |
|
All |
级联Save、Update、Delete操作 |
|
SaveUpdate |
级联Save、Update操作 |
|
Delete |
级联Delete操作 |
OuterJoin也是一个枚举类型,有以下成员:
|
Auto |
自动,默认类型 |
|
True |
进行外连接 |
|
False |
不进行外连接 |
HasMany
HasMany用来完成一对多的映射,如一个Blog有多个post:
HasMany 有以下属性:
|
Access |
可选 - 默认值为 property,用来访问属性值的策略。 |
|
AccessString |
|
|
Cache |
|
|
Cascade |
指定级联操作,指明哪些操作会从父对象级联到关联的对象 |
|
ColumnKey |
指定对应的外键 |
|
CustomAccess |
|
|
Index |
当RelationType 为map时使用 |
|
IndexType |
当RelationType 为map时使用 |
|
Inverse |
默认是 false,标记这个集合作为双向关联关系中的方向一端 |
|
Lazy |
是否延时加载,booel类型,默认为false |
|
MapType |
|
|
OrderBy |
指定表的字段(一个或几个)再加上asc或者desc(可选), 定义Map,Set和Bag的迭代顺序 |
|
RelationType |
|
|
Schema |
表的schema的名称 |
|
Sort |
指定集合的排序顺序,当RelationType 为set时使用 |
|
Table |
指定持久化类所关联的数据库表名,如果表名与类名相同,可以省略 |
|
Where |
附加的SQL Where子句 |
HasAndBelongsToMany
asAndBelongsToMany 用来完成多对多的关联映射。
看一下下面的表结构:
CREATE TABLE [dbo].[companies] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[client_of] [int] NULL ,
[name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[type] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[people] (
[id] [int] IDENTITY (1, 1) NOT NULL ,
[name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
CREATE TABLE [dbo].[people_companies] (
[person_id] [int] NOT NULL ,
[company_id] [int] NOT NULL
) ON [PRIMARY]
一个company有people,并且person有很多company,使用HasAndBelongsToMany可以实现这种关联映射:
[ActiveRecord("companies")]
public class Company : ActiveRecordBase
{
private int id;
private String name;
private IList _people;
public Company()
{
}
public Company(string name)
{
this.name = name;
}
[PrimaryKey]
public int Id
{
get { return id; }
set { id = value; }
}
[Property]
public String Name
{
get { return name; }
set { name = value; }
}
[HasAndBelongsToMany( typeof(Person),
Table="people_companies",
ColumnRef="person_id", ColumnKey="company_id" )]
public IList People
{
get { return _people; }
set { _people = value; }
}
}
[ActiveRecord("people")]
public class Person : ActiveRecordBase
{
private int _id;
private String _name;
private IList _companies;
public Person()
{
_companies = new ArrayList();
}
[PrimaryKey]
public int Id
{
get { return _id; }
set { _id = value; }
}
[Property]
public string Name
{
get { return _name; }
set { _name = value; }
}
[HasAndBelongsToMany( typeof(Company),
Table="people_companies",
ColumnRef="company_id", ColumnKey="person_id" )]
public IList Companies
{
get { return _companies; }
set { _companies = value; }
}
}
注意一定要指定要指明关联的表和ColumnRef、ColumnKey。
HasAndBelongsToMany 的属性如下表所示:
|
Access |
|
|
AccessString |
|
|
Cache |
|
|
Cascade |
设置或获取 cascade动作,指明了级联操作的类型。 |
|
ColumnKey |
本实体类于另一个实体类关联的外键 |
|
ColumnRef |
另一实体类的外键 |
|
CustomAccess |
|
|
Index |
当RelationType 为map时使用 |
|
IndexType |
当RelationType 为map时使用 |
|
Inverse |
默认是 false,标记这个集合作为双向关联关系中的方向一端 |
|
Lazy |
指定是否延迟加载关联对象 |
|
MapType |
|
|
OrderBy |
指定表的字段(一个或几个)再加上asc或者desc(可选), 定义Map,Set和Bag的迭代顺序 |
|
RelationType |
关系类型 |
|
Schema |
指定Schema的名字 |
|
Sort |
指定集合的排序顺序,当RelationType 为set时使用 |
|
Table |
指定持久化类所关联的数据库表名,如果表名与类名相同,可以省略 |
|
Where |
附加的SQL Where子句 |
OneToOne
一个表与另一个表通过共享主键来完成一对一的关联映射:
[ActiveRecord("Employee")]
public class Employee : ActiveRecordBase
{
private int id;
private Award award;
[PrimaryKey(PrimaryKeyType.Native, "EmployeeID")]
public int ID
{
get { return this.id; }
set { this.id = value; }
}
[OneToOne]
public Award Award
{
get { return this.award; }
set { this.award = value; }
}
}
[ActiveRecord("Award")]
public class Award : ActiveRecordBase
{
private Employee employee;
private int id;
public Award()
{
}
public Award(Employee employee)
{
this.employee = employee;
}
[OneToOne]
public Employee Employee
{
get { return this.employee; }
set { this.employee = value; }
}
[PrimaryKey(PrimaryKeyType.Foreign, "EmployeeID")]
public int ID
{
get { return this.id; }
set { this.id = value; }
}
public static Award[] FindAll()
{
return ((Award[]) (ActiveRecordBase.FindAll(typeof(Award))));
}
public static void DeleteAll()
{
ActiveRecordBase.DeleteAll( typeof(Award) );
}
}
OneToOne有如下属性:
|
Access |
|
|
AccessString |
|
|
Cascade |
|
|
Constrained |
|
|
CustomAccess |
|
|
OuterJoin |
|
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。