摘要:多对多的关系在日常开发中也会经常遇到,在ActiveRecord中我们用HasAndBelongsToMany特性来实现Many-Many的关联,本文将通过一个具体的实例来介绍这一用法。
 
主要内容
1.准备数据库表
2.编写实体类
3.编写测试代码
 
一.准备数据库表
接着在上篇文章中的例子,为了实现多对多的关系,我们引入Community,即每个Blog可以属于多个社区,每个社区也可以有多个Blog
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列CREATE TABLE Blogs (
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列    blog_id     
int IDENTITY(11PRIMARY KEY,
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列    blog_name   
varchar(50),
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列    blog_author 
varchar(50)
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列)
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
CREATE TABLE Blog_Community (
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列     blog_Id 
int NOT NULL ,
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列     community_Id 
int NOT NULL 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列 )
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列 
CREATE TABLE Communities (
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列     community_Id 
int IDENTITY (11PRIMARY KEY,
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列     community_Name 
varchar (50) ,
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列     community_Intro 
varchar (500
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列 )
 
二.编写实体类代码
为了实现多对多的关系,我们要在BlogCommunity类中分别使用HasAndBelongsToMany特性,不需要编写Blog_Community类。示例代码:
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列// Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_33Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_33Blog
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列[HasAndBelongsToMany( 
typeof(Community), 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列        Table
="Blog_Community"
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列        ColumnRef
=" community_id "
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列        ColumnKey
=" blog_id " )]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
public IList Communitys
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_46
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_50    
get return _community; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_54    
set { _ community = value; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_58}

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
// Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_33Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_33Community
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列[HasAndBelongsToMany( 
typeof(Blog), 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列        Table
="Blog_Community"
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列        ColumnRef
="blog_id"
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列        ColumnKey
="community_id" )]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
public IList Blogs
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_76
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_80    
get return _blog; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_84    
set { _ blog = value; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_58}

HasAndBelongsToMany的参数相信大家都能够看明白,指定关联表名和关联的外键就可以了。
注意:这三个参数必须指定,不可以省略!
HasManyAttribute说明
属性
说明
示例
Cascade
指明哪些操作会从父对象级联到关联的对象,相关的操作见后面,如果不指定,则为None
Cascade=ManyRelationCascadeEnum.All
Inverse
指定是否级联操作
Inverse =true|false
Schema
指定Schema的名字
Schema="ARDemo"
Table
指定持久化类所关联的数据库表名,如果表名与类名相同,可以省略
Table="posts"
ColumnKey
本实体类于另一个实体类关联的外键
ColumnKey="community_id"
ColumnRef
另一实体类的外键
ColumnRef="blog_id"
Where
指定一个附加SQLWhere子句
Where="IsPost = 0"
Lazy
指定是否延迟加载关联对象
Lazy=true|false
 
Cascade的类型值有如下几种
类型
说明
None
不进行级联操作
SaveUpdate
进行级联Save/Update操作
Delete
进行级联Delete操作
All
进行级联Save/Update/Delete操作
AllDeleteOrphan
进行级联Save/Update/Delete操作,并删除无相关父对象的子对象
 
最后完整的实体类如下:
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_.Net_89/// <summary>
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
/// Blog 的摘要说明。
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_58
/// </summary>

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列[ActiveRecord(
"Blogs")]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
public class Blog : ActiveRecordBase
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_100
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
private int _id;
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
private String _name;
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
private String _author;
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
private IList _community;
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    [PrimaryKey(PrimaryKeyType.Identity, 
"blog_id")]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public int Id
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_.Net_124    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_128        
get return _id; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_132        
set { _id = value; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    [Property(
"blog_name")]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public String Name
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_.Net_144    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_.Net_148        
get return _name; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_152        
set { _name = value; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    [Property(
"blog_author")]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public String Author
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_164    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_168        
get return _author; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_172        
set { _author = value; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    [HasAndBelongsToMany(
typeof(Community),
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            Table
="Blog_Community",
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            ColumnRef
=" community_id ",
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            ColumnKey
=" blog_id " )]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public IList Communities
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_.Net_190    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_194        
get return _community; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_198        
set { _community = value; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public static void DeleteAll()
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_210    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49        DeleteAll( 
typeof(Blog) );
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public static Blog[] FindAll()
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_222    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49        
return (Blog[]) FindAll( typeof(Blog) );
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public static Blog Find(int id)
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_234    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49        
return (Blog) FindByPrimaryKey( typeof(Blog), id );
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_58}

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_243/// <summary>
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
/// Community 的摘要说明。
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_58
/// </summary>

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列[ActiveRecord(
"Communities")]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
public class Community : ActiveRecordBase
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_254
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
private int _id;
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
private String _name;
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
private String _intro;
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
private IList _blog;
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    [PrimaryKey(PrimaryKeyType.Identity, 
"Community_Id")]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public int Id
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_278    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_282        
get return _id; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_286        
set { _id = value; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    [Property(
"Community_Name")]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public String Name
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_298    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_302        
get return _name; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_306        
set { _name = value; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    [Property(
"Community_Intro")]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public String Author
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_318    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_322        
get return _intro; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_326        
set { _intro = value; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    [HasAndBelongsToMany(
typeof(Blog),
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            Table
="Blog_Community",
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            ColumnRef
="blog_id",
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            ColumnKey
="community_id" )]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public IList Blogs
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_.Net_344    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_348        
get return _blog; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_352        
set { _blog = value; }
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public static void DeleteAll()
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_362    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49        DeleteAll( 
typeof(Community) );
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public static Community[] FindAll()
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_374    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49        
return (Community[]) FindAll( typeof(Community) );
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
public static Community Find(int id)
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_386    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49        
return (Community) FindByPrimaryKey( typeof(Community), id );
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_58}
 
三.编写测试代码
下面是我写的一些简单的测试代码,有兴趣的可以看一下。
1.级联增加:新增一个Blog,让它同时属于好几个社区
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列[Test]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
public void TestCascadingSave()
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_399
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
//新建一个Blog
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    Blog blog 
= new Blog();
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    blog.Name 
= "Tech Space";
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    blog.Author 
= "Terrylee";
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
//属于ID为1,2社区
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    ArrayList list 
= new ArrayList();
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    list.Add(Community.Find(
1));
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    list.Add(Community.Find(
2));
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    blog.Communities 
= list;
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
//保存
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    blog.Save();
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_58}

2.级联更新:对一个已经存在Blog,更改它属于更多的社区
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列[Test]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
public void TestCascadingUpdate()
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_434
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
//测试1:查找一个Blog
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    Blog blog 
= Blog.Find(10);
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    IList clist 
= blog.Communities;
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    clist.Add(Community.Find(
4));
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    clist.Add(Community.Find(
3));
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    blog.Save();
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
//测试2:查找一个Community
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    Community community 
= Community.Find(3);
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    IList blist 
= community.Blogs;
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    blist.Add(Blog.Find(
8));
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    community.Save();
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_58}
 
3.级联删除:删除一个Blog,级联表中对应的记录应该删除,但Community不能删除,因为还有别的Blog和它关联
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列[Test]
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
public void TestCascadingDelete()
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_479
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
//测试1:删除Blog
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    Blog blog 
= Blog.Find(10);
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
using(TransactionScope btran = new TransactionScope())
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_491    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49        
try
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_497        
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            blog.Delete();
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            btran.VoteCommit();
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136        }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49        
catch
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_509        
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            btran.VoteRollBack();
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136        }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
//测试2:删除Community
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    Community community 
= Community.Find(3);
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49 
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49    
using(TransactionScope ctran = new TransactionScope())
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_Castle开发系列_529    
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49        
try
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_映射_535        
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            community.Delete();
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            ctran.VoteCommit();
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136        }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49        
catch
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_547        
{
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49            ctran.VoteRollBack();
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136        }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_136    }

Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_关系_49
Castle ActiveRecord学习实践(5):实现Many–Many关系的映射_实践_58}
 
好了,关于Many-Many关联映射就写这么多了,内容比较简单。下篇文章我会介绍在ActiveRecord中实现延迟加载和使用Where子句。