惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - Franky
云风的 BLOG
云风的 BLOG
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Engineering at Meta
Engineering at Meta
Vercel News
Vercel News
Y
Y Combinator Blog
B
Blog
Microsoft Azure Blog
Microsoft Azure Blog
C
Check Point Blog
M
MIT News - Artificial intelligence
Jina AI
Jina AI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Apple Machine Learning Research
Apple Machine Learning Research
Hugging Face - Blog
Hugging Face - Blog
阮一峰的网络日志
阮一峰的网络日志
罗磊的独立博客
Stack Overflow Blog
Stack Overflow Blog
F
Fortinet All Blogs
博客园 - 司徒正美
I
InfoQ
Google DeepMind News
Google DeepMind News
GbyAI
GbyAI
U
Unit 42

博客园 - 大田

浪潮通软2012年社招一期用人目录 浪潮通软2012年社招二期用人目录 续-在sharepoint中利用文档库扩展新闻应用 BDC tool发布了 一个好用的wsp打包工具-WSPbuilder 在sharepoint中利用文档库扩展新闻应用 服务器端ListItem生成前导空格方法小结 - 大田 - 博客园 一个不被flash、select、activex遮挡的、跨frame的无限分级菜单 贴边隐藏、图形窗体等效果的实现:一个模仿QQ界面的MSN界面 练习WF SqlTrackingService时的一点注意事项 及一点感言 vs2005服用windows work flow后种种不良反应的解决方法 Inspur——赢战2007 asp.net 2.0热贴收藏 美化的窗体界面 可以为空值、可以删除显示日期的DateTimePicker 生命如此脆弱,告全体打工者们 用C#实现智能设备上的NotifyIcon类 再谈“虫子”的问题 “虫子”的问题
Castle ActiveRecord(五) 映射基础之二
大田 · 2006-08-07 · via 博客园 - 大田

 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

 指定持久化类所关联的数据库表名,如果表名与类名相同,可以省略

TypeId

Where

 附加的SQL Where子句

HasAndBelongsToMany

asAndBelongsToMany 用来完成多对多的关联映射。

看一下下面的表结构:

CREATE TABLE [dbo].[companies] (
    
[id] [int] IDENTITY (11NOT 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 (11NOT 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

指定持久化类所关联的数据库表名,如果表名与类名相同,可以省略

TypeId

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

TypeId